?
Solved

Compliation and Section Problem

Posted on 2005-04-26
11
Medium Priority
?
208 Views
Last Modified: 2010-04-15
I am attempting to write a simple program that my partner can use to test her knowledge of medical terminology.

The aim of the program is to:

Accept medical terms and their meanings.
Store this information to disk.

Use the information stored to disk to ask the user questions....

Platform Wintel Microsoft Visual C version 1.52....

the code I have is thus far is below:


Defintion of problem:
1. Volunteered to do this after not touching c for 10 years :-(
2. I get compilation errors when I don't have the _getchar commented out.
3. my switch is going straight to the default and is letting me test the rest of the program.
4. More to be discovered (i'm sure)....

Regarding the number 2 problem, I was playing around with the linker and look at oldnames.lib, but that didn't seem to help - infact I got a whole heap more errors.........

Extra points for some insight regarding the best way to achieve the overall goal.....
I have some problems with the later part, it was dumping the entire contents of file on the screen......



#include <stdio.h>
   #include <stdlib.h>
   #include <conio.h>
   #include <ctype.h>
   
   #define FALSE 0
   #define TRUE !FALSE
   
   
         
    struct question {    // Declares the structure to store the question and answer.
                   
    char term[30];
    char answer[30];
    };
   
   void write_info(void);
   void read_info (void);
   
   void Main()
         {
     
   
         char name[30];    //Participants name.
         int quesnum; //The number of questions that the test will have.
         //int ch;
         int c;
         int done = FALSE;
         
         while(!done)
          
   {
               puts("\nWelcome to a medical terminology test.\n");
               puts("Please input your name.\n");
               gets(name);
               puts("A - Add new terminology\n");
               puts("T - Undertake terminology test\n");
               puts("Q - Quit\n");
         
            // ch = _getch();
            // ch = toupper(ch);
             scanf(c);
            
             switch(c)
             {
                   case ('A'):
                         printf("Add new terminology\n");
                         write_info();
                         break;
                   case ('T'):
                         printf("%s please prepare yourself for the test....\n",name);
                           puts("How many questions would you like?");
                           gets(quesnum);
                           printf("There will be %s questions in this test.\n",quesnum);
                           read_info(); //initiates the reading of the questions from file.
                           break;
                     case ('Q'):
                           puts("Quit\n");
                           done = TRUE;
                           break;
                     default:
                           puts("No match?");
                           break;            
                           
                           
               }
         }
         
                    }
         
         void write_info(void)
         {
          
          
          FILE *quests;                                                       //define the file.
          struct question test;                                           //create an instance of the question structure.
          printf("Enter the medical term:");             //Ask for the medical term.
          gets(test.term);                               //receive term and store in structure.
          printf("Enter the meaning of the term:");      //Ask for the answer to the term.
          gets(test.answer);                             //receive answer and store in structure.
          
          quests = fopen("test.dat","a");                        //Open the file in append mode.
          
          if(quests==NULL)
                {
                      puts("Error opening file");
                      exit(1);
                }
                
                fwrite(&test,sizeof(test),1,quests);
                
                fclose(quests);
                puts("Terminology added.");
          
          
         
         }
         void read_info(void)
         {
               FILE *quests;  
               struct question test;
               char ans[50];
               char a[50];
               char q[50];
               int x;
               int score;
               
               quests = fopen("test.dat","r");
               if(quests==NULL)
                     {
                           puts("No data in file");
                           return;
                     }
               
              while(TRUE)
              {
                     x = fread(&test,sizeof(test),1,quests);
                     //if(x==0)break;
                     
                     printf("\nPlease type the meaning of the following medical terms");
                     //fgets (q,50, quests);
                     //printf("\n%s",q);
                     printf("\n%s",test.term);
                     gets(ans);
                     if (test.answer == ans)
                           {
                           score = score + 1;
                           printf("\n Correct!");
                           }
                     else
                           {
                           printf("\nincorrect!");       
                         }
              
              }    
              fclose(quests);
              
         
         }
0
Comment
Question by:isec1
  • 6
  • 5
11 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13873070
1. Use getchar()
2. You do not need () around case values ... case 'A': would be fine
3. Dont use gets. Use fgets instead. Refer to the man/help page of gets for information on why gets should not be used
4. You might consider using a database like MS Access
0
 

Author Comment

by:isec1
ID: 13873583
Ok, thanks the getchar has worked a treat. Any reason why this part doesn't allow input for the first question?

struct question test;                                    //create an instance of the question structure.
         printf("Enter the medical term:");             //Ask for the medical term.
         gets(test.term);                               //receive term and store in structure.
         printf("Enter the meaning of the term:");      //Ask for the answer to the term.
         gets(test.answer);                             //receive answer and store in structure.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13873588
3. Dont use gets. Use fgets instead. Refer to the man/help page of gets for information on why gets should not be used
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:isec1
ID: 13876460
thanks for you help,

I followed the suggestion regarding using fgets...

fgets(test.term,30,stdin);

I get the same result it seems to skip the first question and goes straight to waiting for the response for the second......
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13876965
That typically means there is some left over data in the input buffer, typically a newline character.

please post the complete exact and current code so that I can suggest a fix
0
 

Author Comment

by:isec1
ID: 13881468
void write_info(void)
         {
          
          
          FILE *quests;                                                       //define the file.
          struct question test;                                           //create an instance of the question structure.
          printf("\nEnter the medical term:");             //Ask for the medical term.
          fgets(test.term,30,stdin);                               //receive term and store in structure.
          
          printf("\nEnter the meaning of the term:");      //Ask for the answer to the term.
          
          getchar(test.answer,30,stdin);                             //receive answer and store in structure.
          
          quests = fopen("test.dat","a");                        //Open the file in append mode.
          
          if(quests==NULL)
                {
                      puts("Error opening file");
                      exit(1);
                }
                
                fwrite(&test,sizeof(test),1,quests);
                
                fclose(quests);
                puts("Terminology added.");
          
          
         
         }
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13881951
I will need main too. Most likely there is a call to scanf or some other input routine which is leaving excess data in the input buffer.
>getchar(test.answer,30,stdin);                             //receive answer and store in structure.
You would like to use fgets here ... Did it compile?
0
 

Author Comment

by:isec1
ID: 13882585
Main is posted below.....


Yes it did compile, and interchanged the fgets and the getchar to see if there was any difference in the output...and the reported problem...and there wasn't..

 void Main()
         {
     
   
         char name[30];    //Participants name.
         int quesnum; //The number of questions that the test will have.
         //int ch;
         int c;
         int done = FALSE;
         
         while(!done)
          
   {
               puts("\nWelcome to a medical terminology test.\n");
               puts("Please input your name.\n");
               gets(name);
               puts("A - Add new terminology\n");
               puts("T - Undertake terminology test\n");
               puts("Q - Quit\n");
         
             c = getchar();
            c = toupper(c);
         
            
             switch(c)
             {
                   case ('A'):
                         printf("\nAdd new terminology");
                         write_info();
                         break;
                   case ('T'):
                         printf("%s please prepare yourself for the test....\n",name);
                           puts("How many questions would you like?");
                           gets(quesnum);
                           printf("There will be %s questions in this test.\n",quesnum);
                           read_info(); //initiates the reading of the questions from file.
                           break;
                     case ('Q'):
                           puts("Quit\n");
                           done = TRUE;
                           break;
                     default:
                           puts("No match?");
                           break;            
                           
                           
               }
         }
         
                    }
         
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13883172
You arte still using gets in the main () !!!!! Input buffer is the same for all functions in your program. Your main leaves behind data and calls write_info(). The first read in write_info() is satisfied by the leftover data ... replace all gets in the program with fgets()
0
 

Author Comment

by:isec1
ID: 13883596
Ok, I have done as you have instructed as is shown below. The results on execute are the same, i.e., it goes straight to the second question.


 #include <stdio.h>
   #include <stdlib.h>
   #include <conio.h>
   #include <ctype.h>
   
   #define FALSE 0
   #define TRUE !FALSE
   
   
         
    struct question {    // Declares the structure to store the question and answer.
                   
    char term[30];
    char answer[30];
    };
   
   void write_info(void);
   void read_info (void);
   
   void Main()
         {
     
   
         char name[30];    //Participants name.
         int quesnum; //The number of questions that the test will have.
         //int ch;
         int c;
         int done = FALSE;
         
         while(!done)
          
   {
               puts("\nWelcome to a medical terminology test.\n");
               puts("Please input your name.\n");
               fgets(name,30,stdin);
               puts("A - Add new terminology\n");
               puts("T - Undertake terminology test\n");
               puts("Q - Quit\n");
         
             c = getchar();
             c = toupper(c);
         
            
             switch(c)
             {
                   case ('A'):
                         printf("\nAdd new terminology");
                         write_info();
                         break;
                   case ('T'):
                         printf("%s please prepare yourself for the test....\n",name);
                           puts("How many questions would you like?");
                           fgets(quesnum,10,stdin);
                           printf("There will be %s questions in this test.\n",quesnum);
                           read_info(); //initiates the reading of the questions from file.
                           break;
                     case ('Q'):
                           puts("Quit\n");
                           done = TRUE;
                           break;
                     default:
                           puts("No match?");
                           break;            
                           
                           
               }
         }
         
                    }
         
         void write_info(void)
         {
          
          
          FILE *quests;                                                       //define the file.
          struct question test;                                           //create an instance of the question structure.
          printf("\nEnter the medical term:");             //Ask for the medical term.
          fgets(test.term,30,stdin);                               //receive term and store in structure.
          
          printf("\nEnter the meaning of the term:");      //Ask for the answer to the term.
          
          fgets(test.answer,30,stdin);                             //receive answer and store in structure.
          
          quests = fopen("test.dat","a");                        //Open the file in append mode.
          
          if(quests==NULL)
                {
                      puts("Error opening file");
                      exit(1);
                }
                
                fwrite(&test,sizeof(test),1,quests);
                
                fclose(quests);
                puts("Terminology added.");
          
          
         
             }
         void read_info(void)
         {
               FILE *quests;  
               struct question test;
               char ans[50];
               char a[50];
               char q[50];
               int x;
               int score;
               
               quests = fopen("test.dat","r");
               if(quests==NULL)
                     {
                           puts("No data in file");
                           return;
                     }
               
              while(TRUE)
              {
                     x = fread(&test,sizeof(test),1,quests);
                     //if(x==0)break;
                     
                     printf("\nPlease type the meaning of the following medical terms");
                     //fgets (q,50, quests);
                     //printf("\n%s",q);
                     printf("\n%s",test.term);
                     fgets(ans,30,stdin);
                     if (test.answer == ans)
                           {
                           score = score + 1;
                           printf("\n Correct!");
                           }
                     else
                           {
                           printf("\nincorrect!");       
                         }
              
              }    
              fclose(quests);
              
         
         }
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 1500 total points
ID: 13883713
Your parameters to fgets are incorrect .... It takes char * , the buffer in which to store data as first argument. you are passing it an uninitialized int!!!!!

I am surprised that your code compiles because you do not have a main() ... main is different from Main. C is case sensitive.

Here is part of reworked code ... It needs lots of improvements especially the read_info part

        int c,d;
        int done = FALSE;
        puts("\nWelcome to a medical terminology test.\n");
        puts("Please input your name.\n");
        fgets(name,30,stdin);
       
        while(!done)
         
   {

             puts("A - Add new terminology\n");
             puts("T - Undertake terminology test\n");
             puts("Q - Quit\n");
       
            c = getchar();
            c = toupper(c);
                  d= getchar();
       
           
            switch(c)
            {
                 case ('A'):
                      printf("\nAdd new terminology");
                      write_info();
                      break;
                 case ('T'):
                      printf("%s please prepare yourself for the test....\n",name);
                       puts("How many questions would you like?");
                       fgets(buffer,10,stdin);
                                 quesnum = atoi(buffer);
                       printf("There will be %s questions in this test.\n",quesnum);
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question