• C

Compliation and Section Problem

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);
              
         
         }
isec1Asked:
Who is Participating?
 
sunnycoderConnect With a Mentor Commented:
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
 
sunnycoderCommented:
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
 
isec1Author Commented:
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
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
sunnycoderCommented:
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
 
isec1Author Commented:
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
 
sunnycoderCommented:
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
 
isec1Author Commented:
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
 
sunnycoderCommented:
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
 
isec1Author Commented:
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
 
sunnycoderCommented:
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
 
isec1Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.