Whats wrong w/ my loop?? Please help?

This is a discussion on Whats wrong w/ my loop?? Please help? within the C Programming forums, part of the General Programming Boards category; Hey Guys, This is my function..when I put in 'b' or 'c' or 't' it works just fine..type in 'j' ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    63

    Whats wrong w/ my loop?? Please help?

    Hey Guys,
    This is my function..when I put in 'b' or 'c' or 't' it works just fine..type in 'j' and the output looks like this..Why is it doing that..help please! Thanks
    Aspan

    Output:
    What kind of car would you like to park?
    (C)ar or (B)us or (T)ruck:j
    What kind of car would you like to park?
    (C)ar or (B)us or (T)ruck:
    What kind of car would you like to park?
    (C)ar or (B)us or (T)ruck:

    CODE:
    char inputCar(void)
    {

    char c;
    char car;


    printf("What kind of car would you like to park?\n");
    printf("(C)ar or (B)us or (T)ruck:");
    scanf("%c", &car);
    c = toupper(car);

    while(!(c == 'C' || c == 'B' || c == 'T'))
    {
    printf("What kind of car would you like to park?\n");
    printf("(C)ar or (B)us or (T)ruck:\n");
    scanf("%c", &car);
    c = toupper(car);
    }


    return c;
    }

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    41
    What happens if you fflush() the stdin after each scanf()? Maybe it interprets the "enter" as another key or something like that? =) Actually I have no clue so it's just a thought.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    The problem is not you. It's with scanf(). Try getting it as a string with fgets, then either use strstr(), !strmp(), or extract the char from the string and compare as you did. The code below worked.

    Code:
    
    
    char inputCar(void) 
    {
    char c; 
    char car[5];
    
    do{
    
    printf("What kind of car would you like to park?\n"); 
    printf("(C)ar or (B)us or (T)ruck:\n"); 
    fgets(car, 5, stdin);
    c = toupper(car[0]);
    
    }while(c != 'C' && c != 'B' && c != 'T');
    
    return c; 
    }
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    Sparks suggestion worked.

    Code:
    
    
    char inputCar(void) 
    {
    char c; 
    char car;
    
    do{
    fflush(stdin);
    printf("What kind of car would you like to park?\n"); 
    printf("(C)ar or (B)us or (T)ruck:\n"); 
    scanf("%c", &car);
    c = toupper(car);
    }while(c != 'C' && c != 'B' && c != 'T');
    
    return c; 
    }
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What happens if you fflush() the stdin after each scanf()? Maybe it interprets the "enter" as another key or something like that? =) Actually I have no clue so it's just a thought.
    Sparks suggestion worked.
    fflush(stdin) is bad coding. This only gets highlighted like, once a day!

    Use a portable solution like:
    Code:
    ...
    scanf("%c", &car);
    while (getchar() != '\n');
    ...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    41
    Originally posted by Hammer


    fflush(stdin) is bad coding. This only gets highlighted like, once a day!
    Damn, I knew my book about C/C++ would be crap.

    This is how the author suggests to do keyboard input:

    Code:
    printf( "Please enter two numbers: ");
    scanf( "%d %d", &num1, &num2);
    fflush( stdin );
    Lucky me that I didn't actually read this book.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Lucky me that I didn't actually read this book.
    Burn it.... burn it now!!!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 04:49 PM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  5. What is wrong with my while loop?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-19-2002, 12:07 PM

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