Thread: Try again decision problem

  1. #1
    Registered User hawkeye10's Avatar
    Join Date
    Feb 2012
    Posts
    3

    Try again decision problem

    Hi guys! can you help a novice programmer here. I've been working on a Try again decision using the Do-While loop. Heres the code I've established.

    Code:
    #include <stdio.h>
    
    int main(void)
    
    {
    
    
    // Declaration of Variables
    double Pounds = 0;
    double Kilograms = 0;
    double Inches = 0;
    double Meters = 0;
    double BMI = 0;
    
    char ans1 = ' ';
    char ans2 = ' ';
    char ans3 = ' ';
    
    
        do
        {
            {
            printf("Please enter your height(Meters): ");
            scanf("%lf", &Meters);
                
            printf("Please enter your weight(Kilograms): ");
            scanf("%lf", &Kilograms);
                
            BMI = Kilograms/(Meters*Meters);
                
            printf("Your BMI (Meters&Kilograms) is: %.2f\n", BMI);
            }
        
        printf("Would you like to try again? (y/n): ");
        scanf("%c", &ans1);
        fflush(stdin);
        }
        while (ans1 =='y');
        
    }
    
    The whole thing works until I reach the try again decision.

    Code:
    Please enter your height(Meters): 
    1.6
    Please enter your weight(Kilograms): 
    52
    Your BMI (Inches&Pounds) is: 20.31
    Would you like to try again? (y/n): 
    Program ended with exit code: 0
    This is what I always end up with. I would really appreciate the help

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Call fflush(stdin) before every scanf()

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by DRK View Post
    Call fflush(stdin) before every scanf()
    No! Never call fflush(stdin). Read this link: FAQ > Why fflush(stdin) is wrong - Cprogramming.com.

    @OP:
    Note, when you hit enter, like you do after entering your height/weith, that puts a new line ('\n') in the input buffer. scanf, when used with the %c, doesn't skip leading white space like the other modifiers (%d, %lf, etc) do. That means when you scan a char into ans1, you scan in the '\n'. You can force scanf to skip any leading whitespace, including '\n', by putting a space before the %c, like scanf(" %c", &ans1).

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Sorry, my advice wasn't correct, I was sugested by fflush(stdin) already used in the example code.

  5. #5
    Registered User hawkeye10's Avatar
    Join Date
    Feb 2012
    Posts
    3
    Quote Originally Posted by anduril462 View Post
    No! Never call fflush(stdin). Read this link: FAQ > Why fflush(stdin) is wrong - Cprogramming.com.

    @OP:
    Note, when you hit enter, like you do after entering your height/weith, that puts a new line ('\n') in the input buffer. scanf, when used with the %c, doesn't skip leading white space like the other modifiers (%d, %lf, etc) do. That means when you scan a char into ans1, you scan in the '\n'. You can force scanf to skip any leading whitespace, including '\n', by putting a space before the %c, like scanf(" %c", &ans1).
    Got it! Thanks for the help dude!

    Code:
    Please enter your height(Meters): 1.62
    Please enter your weight(Kilograms): 59
    Your BMI (Inches&Pounds) is: 22.48
    Would you like to try again? (y/n): y
    Please enter your height(Meters): 
    

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a good decision?
    By Masterx in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 02-14-2009, 12:13 PM
  2. Decision Time....
    By korjiro in forum C++ Programming
    Replies: 16
    Last Post: 07-13-2004, 04:20 PM
  3. The decision
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-09-2003, 03:23 AM
  4. Tough decision - help!
    By da_fall_guy in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2003, 10:10 AM
  5. Help - Decision in a do loop
    By dp174 in forum C Programming
    Replies: 3
    Last Post: 12-05-2002, 02:40 PM