Reputation: 41
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() */
Upvotes: 2
Views: 22038
Reputation: 9997
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);
Upvotes: 0
Reputation: 1281
just put getchar()
after scanf
statement of yours that will eat the unnecessary '\n'
from buffer...
Upvotes: 0
Reputation: 57639
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);
.
Upvotes: 4
Reputation: 2613
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();
Upvotes: 2