Do While error prints twice despite scanf

This is a discussion on Do While error prints twice despite scanf within the C Programming forums, part of the General Programming Boards category; I am trying to teach myself C and decided to make a console project that would be a Basal Metabolic ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    Do While error prints twice despite scanf

    I am trying to teach myself C and decided to make a console project that would be a Basal Metabolic Weight/BMI/Calorie Calculator. Anyways I am stumped early on with this problem. Can anyone tell me where I went wrong? I have been racking my brain but haven't been able to figure it out. (But I am somewhat new at this) Thanks!!!

    Melissa

    Code:
    #include <stdio.h>
    
    int main()
    {
        char Gender = 'a';
        char tempGender;
    
        //Title
        printf("WEIGHT LOSS CALCULATOR \n\n\n");
        //Gather gender info
        do {
            printf("Are you male or female? \nType M for male and F for female.\n\n"); 
            scanf("%c", &tempGender);
            //printf("%c",tempGender\n);
    
            if (tempGender == 'm' || tempGender == 'M') {
                Gender = 'm';
                printf("You are a male.\n\n");
            }
            else if (tempGender == 'f'|| tempGender == 'F') {
                Gender = 'f';
                printf("You are a female.\n\n");
            }
            else {
                printf("ERROR. Please try again.\n\n");
                printf("You typed: %c\n",tempGender);
    
            }
            }while (Gender == 'a');
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,551
    Code:
    scanf("%c", &tempGender);
    Instead try this; the leading space means skip space like the newline.
    Code:
    scanf(" %c", &tempGender);
    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Thanks! That worked! Should I always put that space? Really Thank you

    Melissa

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by mreneeholland View Post
    Should I always put that space?
    No. Putting the space is an (ugly) workaround about the misuse of the scanf() function.
    What you should do is understand why the space works, and what are the better alternatives.

    1) scanf() was designed to work with formatted data (and it has very limited error validation/checking/recovery).
    2) User input has no resemblance at all to formatted data
    3) scanf() and user input are mostly incompatible
    4) prefer to use fgets() to get a full line from the user
    5) parse that line with, for example, sscanf().
    6) ignore the fact your program is a few lines longer: it is also very many times more solid.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Thank you for taking the time to explain this to me. I will definitely run through more tutorials and try to get a better understanding between the different syntax options and uses. I have only done a little Visual Basic at a beginner level. Thank you again

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    You can also do this

    Code:
    scanf("%c%*c",  &tempGender);
    The %*c reads the new line character (left over from when you hit enter) and then does nothing with it (basically so it is not left on the input).

    scanf returns the number of successful inputs - So you can monitor if your user was successful in inputting a number.

    Using fgets and then sscanf is a long way of doing the same thing - I don't think that it's necessary.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-06-2011, 01:08 AM
  2. scanf() error checking
    By Dangerous Dave in forum C Programming
    Replies: 7
    Last Post: 11-26-2003, 05:02 PM
  3. Error Checking for scanf
    By Dangerous_Dave in forum C Programming
    Replies: 2
    Last Post: 11-12-2003, 10:30 AM
  4. Need help error capturing using scanf
    By Pauleb in forum C Programming
    Replies: 4
    Last Post: 01-16-2002, 12:56 PM
  5. Check for error in scanf()
    By Dangerous Dave in forum C Programming
    Replies: 5
    Last Post: 12-08-2001, 05:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21