The loop does not repeat itself! Why?

This is a discussion on The loop does not repeat itself! Why? within the C Programming forums, part of the General Programming Boards category; I am trying to use a sentinel value to control the loop. However, if I set the sentinel value to ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    The loop does not repeat itself! Why?

    I am trying to use a sentinel value to control the loop. However, if I set the sentinel value to be a char, to be entered from keyboard, the loop never repeats itself. But if I set the sentinel value to be an int such as 1, it worked.

    The code is as following. Could you please help me out?



    Code:
     
    Code:
    int main(void)
    {
        double miles, kms;
        char sentinel;
        printf("Run the conversion program (y/n)?  ");
        scanf("%c", &sentinel);
        
        while (sentinel=='y')
        {
              printf("Enter the distance in miles>  ");
              scanf("%lf", &miles);
              kms=miles*1.609;
              printf("You traveled %.3f kilometers.\n\n", kms);
              
                    printf("Run the conversion program (y/n)?  ");
              scanf("%c", &sentinel);
        }          
        
        return (0);
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Are you by any chance working with your caps lock turned on... Y (capital) and y (lower) are two different things.

    Try it with...
    Code:
    while (toupper(sentinel) == 'Y')
    A little trick with scanf("%c"... is to add a leading space before the % sign as in scanf(" %c"... which tells it to ignore everything before the first visible character.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,939
    Use a debugger to find out what value was read into sentinel. How is this value connected to the previous scanf call?

    Alternatively, print the value of sentinel, as an int, after the loop ends to find out what it was.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    I added a leading space before % in the scanf statement. Then it worked. But I don't know why.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    A whitespace before %c 'eats' all heading whitespaces from your input-line. The first not whitespace without '\n' yourself will be putted in your char variable.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    while (sentinel=='y')
    {
        printf("Enter the distance in miles>  ");
        scanf("%lf", &miles);
        kms=miles*1.609;
        printf("You traveled %.3f kilometers.\n\n", kms);
               
        printf("Run the conversion program (y/n)?  ");
        scanf("%c", &sentinel);
    }
    The why... at the point where you ask the user to enter the miles, they enter a value and press enter. The scanf function converts the value and stores it into the miles variable but the newline character remains in the input buffer. Later, when you ask the user if they want to go again, you try to get a character. The leftover newline is a character and is what the scanf function reads. Since the newline character is not 'y' the loop exits and this is why you need the space in that particular scanf call. You gotta be careful when using %c in a scanf call as it will accept and convert any data that happens to be in the input buffer at the time.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. repeat until any key
    By jatoo in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 02:55 AM
  2. repeat loop
    By taurus in forum C Programming
    Replies: 14
    Last Post: 09-11-2007, 07:31 AM
  3. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  4. repeat
    By linuxdude in forum C Programming
    Replies: 4
    Last Post: 03-28-2003, 08:40 AM
  5. same error repeat
    By razoblade in forum C Programming
    Replies: 1
    Last Post: 11-16-2001, 11:50 AM

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