Thread: Using getc(stdin) or getchar(c)

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    help w/ --> Using getc(stdin) or getchar(c)

    I'm currently enrolled in a ANSI C course at my college and i'm having an issue with a particular portion of the assignment...

    The Overview:

    The user needs to be able to input a 'y', 'Y', 'n', 'N' as a response to 5 questions, but my program must also be able to accept Yes, NO, YUP, or NOPE, and similar examples, as long as the first character in their response is y or n, in their variations. I'm currently using:

    scanf(" %c", &response) to get their response.

    Problem is, my instructor wants us to use the getc(stdin) or getchar(c).

    Since i am very new to C, the "advance" terms are beyond me, as well as we are not allowed to use them. Either way, i don't undertand the use of or how to use getc or getchar, the example programs are unclear at times and don't necessarily coincide with my use of them. here the same of what i have so far, i just need some direction on how to use getc or getchar inplace of my scanf, i'll copy my program here, it's not all that big. I'm a noob at this so please be nice


    Code:
    ---------------------------------------------------------------------------------
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    /* Variables
    * 
    *    Patient Number - patNum
    *  Patient Number (Left to Right)
    *    First Digit    - d1
    *    Second Digit   - d2
    *    Thrid Digit    - d3
    *    Fourth Digit   - d4
    *    Fifth Digit    - d5
    *    Sixth Digit    - d6
    *
    *    Modulous Digit - modDig
    *    Patient# Temp  - temp
    *
    */
    
    int patNum = 0, temp, lpCtrl, symptoms;
    int fever, headache, soreThroat, cough, sniffles, sneezes;
    int fCount, hCount, stCount, cCount, sniCount, sneCount;
    int d1 = 0, d2 = 0, d3 = 0, d4 = 0, d5 = 0, d6 = 0;
    int odd_d1, odd_d3, odd_d5, sum,modSum, divSum, check, check_1, count;
    char fever_rep, headache_rep, soreThroat_rep, cough_rep, sniffles_rep, sneezes_rep;
    
    main()
    {
      printf("Clark Hospital Diagnositic Center \n\n");
     
       for(;;)
    /***/{
    
          printf("\n Enter the patient number (0 to exit): ");
          scanf(" %i", &patNum);
    
          if(patNum == 0)
           {
            printf("\n User has ended program \n"
                   " Program will now terminate");
            exit(1);
           }
    
          temp = patNum;
    
          /* Patient Number Extraction */    
    
          d6 = temp % 10;
          temp = temp / 10;
          printf(" %i", d6);
    
          d5 = temp % 10;
          temp = temp / 10;
          printf(" %i", d5);
    
          d4 = temp % 10;
          temp = temp / 10;
          printf(" %i", d4);
    
          d3 = temp % 10;
          temp = temp / 10;
          printf(" %i", d3);
      
          d2 =temp % 10;
          temp = temp / 10;
          printf(" %i", d2);
    
          d1 = temp % 10;
          temp = temp / 10;
          printf(" %i", d1);      
    
              
          /* Patient Number Verificaton */
    
          odd_d1 = d1 * 2;
          odd_d3 = d3 * 2;
          odd_d5 = d5 * 2;
    
          if(odd_d1 > 10)
            odd_d1 = (odd_d1 % 10) + 1;
         
    
          if(odd_d3 > 10)
            odd_d3 = (odd_d3 % 10) + 1;
    
          if(odd_d5 > 10)
            odd_d5 = (odd_d5 % 10) + 1;
    
        
          sum = odd_d5 + odd_d3 + odd_d1 + d4 + d2;
    
          modSum = sum % 10;
          divSum = sum / 10;
          check = (divSum + 1) * 10;
          check_1 = check - sum;
          
          if(check_1 == d6)
            break;
    
          printf("\n The Patient Number %i is invalid \n", patNum);
          
         
    /***/}    
    
      printf("\nWhich of the following symptoms does the patient have (Y for yes, N for no)\n\n");
    
      /* Fever Input Response */
      
      while(fCount == 0)
      {
      printf("     Fever -------- ");
      scanf(" %c", &fever_rep);
         if(fever_rep == 'Y' || fever_rep == 'y')
          {
           symptoms = symptoms + 1;
           fever = 1;
           fCount = 1;
           break;
          }        
         if(fever_rep == 'N' || fever_rep == 'n')
          {
           fever = 0;
           fCount = 1;
           break;
          }
         if((fever_rep != 'Y' || fever_rep != 'y') || (fever_rep != 'N' || fever_rep != 'n'))
          {
           printf("\n      Invalid Entry\n");
           continue;
          }
       }
    
      /* Headache Input Response */
      
      while(hCount == 0)
      {
      printf("     Headache ----- ");
      scanf(" %c", &headache_rep);
         if(headache_rep == 'Y' || headache_rep == 'y')
          {
           symptoms = symptoms + 1;
           headache = 1;
           hCount = 1;
           break;
          }        
         if(headache_rep == 'N' || headache_rep == 'n')
          {
           headache = 0;
           hCount = 1;
           break;
          }
         if((headache_rep != 'Y' || headache_rep != 'y') || (headache_rep != 'N' || headache_rep != 'n'))
          {
           printf("\n      Invalid Entry\n");
           continue;
          }
       }
    
      /* Sore Throat Input Response */
      
      while(stCount == 0)
      {
      printf("     Sore Throat -- ");
      scanf(" %c", &soreThroat_rep);
         if(soreThroat_rep == 'Y' || soreThroat_rep == 'y')
          {
           symptoms = symptoms + 1;
           soreThroat = 1;
           stCount = 1;
           break;
          }        
         if(soreThroat_rep == 'N' || soreThroat_rep == 'n')
          {
           soreThroat = 0;
           stCount = 1;
           break;
          }
         if((soreThroat_rep != 'Y' || soreThroat_rep != 'y') || (soreThroat_rep != 'N' || soreThroat_rep != 'n'))
          {
           printf("      Invalid Entry\n");
           continue;
          }
       }
    
      /* Cough Input Response */
      
      while(cCount == 0)
      {
      printf("     Cough -------- ");
      scanf(" %c", &cough_rep);
         if(cough_rep == 'Y' || cough_rep == 'y')
          {
           symptoms = symptoms + 1;
           cough = 1;
           cCount = 1;
           break;
          }        
         if(cough_rep == 'N' || cough_rep == 'n')
          {
           cough = 0;
           cCount = 1;
           break;
          }
         if((cough_rep != 'Y' || cough_rep != 'y') || (cough_rep != 'N' || cough_rep != 'n'))
          {
           printf("\n      Invalid Entry\n");
           continue;
          }
       }
    
        
    
    
       /* Sniffles Input Response */
       
       while(sniCount == 0)
       {
       printf("     Sniffles ----- ");
       scanf(" %c", &sniffles_rep);
          if(sniffles_rep == 'Y' || sniffles_rep == 'y')
           {
            symptoms = symptoms + 1;
            sniffles = 1;
            sniCount = 1;
            break;
           }        
          if(sniffles_rep == 'N' || sniffles_rep == 'n')
           {
            sniffles = 0;
            sniCount = 1;
            break;
           }
          if((sniffles_rep != 'Y' || sniffles_rep != 'y') || (sniffles_rep != 'N' || sniffles_rep != 'n'))
           {
            printf("\n      Invalid Entry\n");
            continue;
           }
        }
    
    
       /* Sneezes Input Response */
       
       while(sneCount == 0)
       {
       printf("     Sneezes ------ ");
       scanf(" %c", &sneezes_rep);
          if(sneezes_rep == 'Y' || sneezes_rep == 'y')
           {
            symptoms = symptoms + 1;
            sneezes = 1;
            sneCount = 1;
            break;
           }        
          if(sneezes_rep == 'N' || sneezes_rep == 'n')
           {
            sneezes = 0;
            sneCount = 1;
            break;
           }
          if((sneezes_rep != 'Y' || sneezes_rep != 'y') || (sneezes_rep != 'N' || sneezes_rep != 'n'))
           {
            printf("\n      Invalid Entry\n");
            continue;
           }
        }
    
         
    
    }
    -------------------------------------------------------------------------
    Last edited by daedenilus; 11-12-2005 at 11:43 PM. Reason: Clearity

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps search this forum for getchar. It comes up more frequently than you might imagine.

    [edit]BTW, it's mighty nice to see code in someone's first post within code tags!

    [edit=2]Something to mess with:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
       int reply;
       do {
          fputs("prompt> ", stdout);
          fflush(stdout);
          reply = getchar();
          do { int ch; while( (ch = getchar()) != '\n' && ch != EOF ); } while(0);
       } while ( toupper(reply) == 'Y' );
       return 0;
    }
    
    /* my output
    prompt> yup
    prompt> yes
    prompt> yellow
    prompt> no
    */
    Last edited by Dave_Sinkula; 11-12-2005 at 11:11 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    I follow directions

    just not very well at times

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    we haven't been introduced to the fflush or comparing to EOF. I'm restricted to material up to a certain point, i'm googled getc & getchar. I got confused on most of it

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by daedenilus
    we haven't been introduced to the fflush or comparing to EOF. I'm restricted to material up to a certain point, i'm googled getc & getchar. I got confused on most of it
    That's too bad. One of the possible return values of getchar is EOF. So it's covered as soon as you read about getchar.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    Quote Originally Posted by Dave_Sinkula
    That's too bad. One of the possible return values of getchar is EOF. So it's covered as soon as you read about getchar.
    I don't quite understand how EOF applies to an input from a keyboard, and will getchar() take care of only the first letter of the string, and ignore the rest so it doesn't carry it down through the rest of the inputs... when i type 'yes' on my keboard when i run my posted program, the input seems to be carried down through the next two responses. I even typed 'yyyyyyy' and it completed the rest of my program for the requested input responses.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    EOF is a value that is returned if there is an error. It's good to get to know that getchar is not guaranteed to give you anything.

    The do...while loop is what I used to "eat up" characters beyond the first. There are FAQs here on such subjects.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    With my posted code... After i input the patient's number "453282" and the program checks it and finds that it's a valid number, it hits symptom portion of my code, for some reason, it goes directly through the loop as if i entered something and gives me a "Invalid Response" message. What am i missing besides and education and a brain.
    Last edited by daedenilus; 11-13-2005 at 12:17 AM. Reason: clarity

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're mixing types of input reading. When you type something in, you hit enter. Depending on what you're using to read input, this often times gets left in the input stream. Your next call to reading input sees it.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    can you point me in the direction for what i should look up to fix this problem.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    okay, i just resolved the problem of the "invalid response" coming up, but i'm still trying to figure out how to stop 'Yes' or any other strings of characters from bring up multiple "invalid responses" or continuing through my loop, like inputting 'yyyyyy' and exiting completely out of my while loop. I used

    Code:
    fever_rep = toupper(getchar());
    whether my instructor likes it or not...

    it's hard to find what i'm looking for in FAQ when i don't know what i'm looking for to begin with. Terminology seems to be key with the search function.

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed