do while loop for Yes/No user prompt

19,960

Solution 1

The problem is probably, that you're getting something that isn't y from getchar and the loop exits, as the condition is not matched.

getchar() may use a buffer, so when you type 'y' and hit enter, you will get char 121 (y) and 10 (enter).

Try the following progam and see what output you get:

#include <stdio.h>

int main(void) {
    char c = 0;

    while((c=getchar())) {
        printf("%d\n", c);
    }
    return 0;
}

You will see something like this:

$ ./getchar 
f<hit enter>
102
10

What you can see is that the keyboard input is buffered and with the next run of getchar() you get the buffered newline.

EDIT: My description is only partially correct in terms of your problem. You use scanf to read the number you're testing against. So you do: number, enter, y, enter.

scanf reads the number, leaves the newline from your enter in the buffer, the response = getchar(); reads the newline and stores the newline in response, the next call to getchar() (to strip the newline I described above) gets the 'y' and your loop exits.

You can fix this by having scanf read the newline, so it doesn't linger in the buffer: scanf("%d\n", &number);.

Solution 2

When reading input using scanf (when you enter your number above), the input is read after the return key is pressed but the newline generated by the return key is not consumed by scanf.

That means your first call to getchar() will return the newline (still sitting in the buffer), which is not a 'Y'.

If you reverse your two calls to getchar() - where the second one is the one you assign to your variable, your program will work.

printf("Do you want to try another number? Say Y(es) or N(o): \n");
getchar(); // the newline not consumed by the scanf way above 
response = getchar();
Share:
19,960
Tucker Sampson
Author by

Tucker Sampson

Updated on June 14, 2022

Comments

  • Tucker Sampson
    Tucker Sampson almost 2 years

    My program which finds prime factors is all set...the only thing left that I need to do is this type of output:

    Do you want to try another number? Say Y(es) or N(o): y //asks for another number (goes through the program again)

    Do you want to try another number? Say Y(es) or N(o): n //Thank you for using my program. Good Bye!

    I have my attempt at this below...When I type n it does the correct output. But if I type 'y' it just says the same thing n does....How can I loop the entire program without putting the code for the program inside this while loop I have? So when I press y it goes through the program again?

    int main() {
      unsigned num;
      char response;
    
      do{
        printf("Please enter a positive integer greater than 1 and less than 2000: ");
        scanf("%d", &num);
        if (num > 1 && num < 2000){
          printf("The distinctive prime facters are given below: \n");
          printDistinctPrimeFactors(num);
          printf("All of the prime factors are given below: \n");
          printPrimeFactors(num);
        }
        else{
          printf("Sorry that number does not fall within the given range. \n");
        }
        printf("Do you want to try another number? Say Y(es) or N(o): \n");
        response = getchar();
        getchar();
      }
      while(response == 'Y' || response == 'y');
      printf("Thank you for using my program. Goodbye!");
    
      return 0;
    } /* main() */
    
  • Matt Runion
    Matt Runion about 11 years
    No, recursion is not the answer to tis issue.
  • Matt Runion
    Matt Runion about 11 years
    Yes, getchar() returns an integer. In your while statement, compare the values to the integer ASCII values for Y and y.
  • nemo
    nemo about 11 years
    Sorry, I can't elaborate what you want me to do. Could you a bit more specific?
  • James Boutcher
    James Boutcher about 11 years
    About three hours ago, I finally gained the reputation here to cast a down vote. I thought I may never use it. Thank you for letting me do it once.
  • Chirag Desai
    Chirag Desai about 11 years
    "How can I loop the entire program without putting the code for the program inside this while loop I have? So when I press y it goes through the program again?" I have simply given answer for these two statements of him... mrunion you can try the sample code I have given and if that doesnt work you can press down button...
  • James Boutcher
    James Boutcher about 11 years
    Your solution does not work (and I tested it). The issue has nothing to do with flow and branching of his code. It's all about how response=getchar() is returning a leftover newline in the input buffer from the scanf above.
  • Chirag Desai
    Chirag Desai about 11 years
    ahhh ok..!! I cast the question wrongly..!! I removed all of his code from if part... sorry for misunderstanding!!
  • Matt Runion
    Matt Runion about 11 years
    I have no doubt your code runs, @Chirag Desai. But recursion is not the proper method for handling this type of issue. Why fill the stack with all the function pointers? Why delve into numerous recursive calls for something as simple as a terminating while loop? Recursion is not the proper approach in solving this problem. That's all I meant.
  • James Boutcher
    James Boutcher about 11 years
    ... and now, you've edited your answer so much that this commentary after it makes absolutely no sense.