Thread: "User Input" not working as intended

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

    Exclamation getchar() to grab the first character of a string

    Okay, I've looked through the FAQ's regarding getchar, most if it is a above my level, and the stuff that isn't does not help me at all.

    I've been working on this for the pas 6-7hrs and I'm not getting any closer to the solution than when i first started.

    /* The Situation */

    After the Patient Number check is complete, the diagnosis portion of the program begins, it is in this part i'm having this biggest problem. When the user inputs "yes" or "yupe", "nope" or "no", my program is suppose to just look at the first character of the string they inputed and determine if the response is a "Y" or "N".

    No matter which loop i used, it still doesn't work, I've tried a while, for, do/while, switch. I just don't know what is wrong with it. Anyhow, after typing e.g. "Yes", my program will display this....

    Fever ----------
    Invalid Entry.
    Fever ----------

    it is on the second Fever, that i'm allowed to actually make my first input. And when i do make and input, it will do the same thing for the next symptom....

    All i want to be able to do, is accept a string or a character as an input to the various types mentioned above, if it doesn't fall into this category, it gives a "Invalid Entry" msg, and restates the " Fever ------" prompt.

    I need something. FAQ's and Google just isn't working for me.

    Here's my complete code minus comments and beautification

    Code:
     
    
    
      
    
      printf("\nWhich of the following symptoms does the patient have (Y for yes, N for no)\n");
    
    /*********************************************************/
      /* Fever Input Response */
    
      do
       {
        printf("\n     Fever -------- ");
        getchar();
        fever_rep = toupper(getchar());
        
    
        switch(fever_rep)
          {
           case('Y') : fever = 1;
                       fCount = 1;
                       symptoms = symptoms + 1;
                       break;
           case('N') : fever = 0;
                       fCount = 1;
                       break;
             default : printf("\n   Invalid Response\n");
                       break;
          } 
       } while(fCount == 0);
    
    
    /********************************************************/
      /* Headache Input Response */
    
      do
       {
        printf("\n     Headache -------- ");
        getchar();
        headache_rep = toupper(getchar());
        
    
        switch(headache_rep)
          {
           case('Y') : headache = 1;
                       hCount = 1;
                       symptoms = symptoms + 1;
                       break;
           case('N') : headache = 0;
                       hCount = 1;
                       break;
             default : printf("\n   Invalid Response\n");
                       break;
          } 
       } while(hCount == 0);
    
    
    /**********************************************************/
    Last edited by daedenilus; 11-13-2005 at 05:08 PM. Reason: new code

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    getchar() returns the next available key from the keyboard buffer. If the keyboard buffer is empty, then it will wait for you to type something and press <Enter> key. So, if you type "Yes" <Enter> then each time getchar() is called it will return the next key in the sequence, or 4 different keys in that example. And your program must get all the keys from the keyboard buffer before attempting to get more input.

    You can accomplish that in a couple ways -- the simplest way is by using fgets() which will get all the keys at one time up to and including the '\n' (same as <Enter>) fgets() will also correctly handle the backspace key.
    Code:
    // buffer to hold keyboard input
    char input[20];
    // get all the keys
    fgets(input, sizeof(input), stdin);
    // convert first character to upper-case
    input[0] = toupper(input[0])
    // check for 'Y' or 'N'
    if(input[0] == 'Y' || input[0] == 'N')
    {
       // do something
    }
    or if you want to get the keys one at a time -- code below will not correctly handle the backspace key.

    Code:
    // buffer to hold keyboard input
    char input[20] = {0};
    int index = 0, c;
    // get all the keys
    while(index < (sizeof(input)-1) &&  (c = getchar()) != '\n')
      input[index++] = c;
    // convert first character to upper-case
    input[0] = toupper(input[0])
    // check for 'Y' or 'N'
    if(input[0] == 'Y' || input[0] == 'N')
    {
       // do something
    }

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

    Question using getchar() to grab the first character of a string

    I have to use getchar(c) or getc(stdin), i'm still having problems with typing in a string of characters, i did modify all my loops to (do/while)/Switch, and i've compensated for the initial "Invalid Response" but i'm still at a loss as to why my Loop won't handle the string of characters, with the example of 'yes' or 'nnnn'.

    If i use 'yes' i'll get this response

    Fever ---------- yes
    Headache ----
    invalid response
    Headache ----
    invalid response
    Headache ---- <------ Where cursor is now


    if i use 'nnnn', I'll get this response

    Fever ---------- nnnn
    Headache ----
    Cough --------
    Sore Throat --
    Sniffles -------- <-------- Where cursor is now


    I don't know what the deal is with my code, is help is greatly appreciated.... I'm a noob, so no advanced stuff.

    The Code

    Code:
    
    
    
      printf("\nWhich of the following symptoms does the patient have (Y for yes, N for no)\n");
    
    /*********************************************************/
      /* Fever Input Response */
    
      do
       {
        printf("\n     Fever -------- ");
        getchar();
        fever_rep = toupper(getchar());
        
    
        switch(fever_rep)
          {
           case('Y') : fever = 1;
                       fCount = 1;
                       symptoms = symptoms + 1;
                       break;
           case('N') : fever = 0;
                       fCount = 1;
                       break;
             default : printf("\n   Invalid Response\n");
                       break;
          } 
       } while(fCount == 0);
    
    
    /********************************************************/
      /* Headache Input Response */
    
      do
       {
        printf("\n     Headache -------- ");
        getchar();
        headache_rep = toupper(getchar());
        
    
        switch(headache_rep)
          {
           case('Y') : headache = 1;
                       hCount = 1;
                       symptoms = symptoms + 1;
                       break;
           case('N') : headache = 0;
                       hCount = 1;
                       break;
             default : printf("\n   Invalid Response\n");
                       break;
          } 
       } while(hCount == 0);
    
    
    /**********************************************************/
    Last edited by daedenilus; 11-13-2005 at 05:09 PM. Reason: updated my code

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. working out...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 04-10-2003, 10:20 AM
  2. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM
  3. getting current working filename
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2002, 03:42 PM
  4. What ive been working one, GL related
    By Eber Kain in forum Game Programming
    Replies: 3
    Last Post: 10-15-2001, 09:20 AM
  5. Newbie working on a lift simulation
    By dethray79 in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2001, 08:28 AM