2

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() */

4 Answers 4

4

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);.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, getchar() returns an integer. In your while statement, compare the values to the integer ASCII values for Y and y.
Sorry, I can't elaborate what you want me to do. Could you a bit more specific?
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();

Comments

0

just put getchar() after scanf statement of yours that will eat the unnecessary '\n' from buffer...

7 Comments

No, recursion is not the answer to tis issue.
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.
"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...
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.
ahhh ok..!! I cast the question wrongly..!! I removed all of his code from if part... sorry for misunderstanding!!
|
0

As others have stated, there is a single '\n' character in the input stream left over from your earlier call to scanf().

Fortunately, the standard library function fpurge(FILE *stream) erases any input or output buffered in the given stream. When placed anywhere between your calls to scanf() and getchar(), the following will rid stdin of anything left in the buffer:

fpurge(stdin);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.