Thread: The loop does not repeat itself! Why?

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,817
    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, 03: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, 09:23 AM
  4. repeat
    By linuxdude in forum C Programming
    Replies: 4
    Last Post: 03-28-2003, 09:40 AM
  5. same error repeat
    By razoblade in forum C Programming
    Replies: 1
    Last Post: 11-16-2001, 12:50 PM