|
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char again;
do{
printf("insert y or Y to repeat");
fflush(stdout);
scanf("%c",&again);
}while(again=='y'||again=='Y');
}
i wrote this code to create a loop that when insert y or Y do the job again but did not work.when i enter first y the loop be end.
|
|
|
|
|
As mentioned in the QA section: Please don't cross post. I decided to answer you here.
Did you take a look at "again" in the debugger? You will recognize that in the second round thru the loop it will have the value 0x0a == \r it's the carriage return from your input.
You can try something like this to catch the CR:
char cr;
char again;
do
{
printf("insert y or Y to repeat");
fflush(stdout);
again = getchar();
cr = getchar();
}while(again=='y'||again=='Y');
|
|
|
|
|
thank you very much Andy411.its work good.
what do you use
cr = getchar();
what do this code
|
|
|
|
|
How do you learn C? Don't you have a book or a tutoroial with an index?
That's what google answered me:
http://www.cplusplus.com/reference/cstdio/getchar/[^]
PS: Sorry if my answer sounds a bit rude, but I am realy confused about the question what getchar does. If I were you, my first step would be asking google, bing are whatever searchmachine you want. Or taking a look inside a book. If I don't understand the description/answer there, I would ask in a forum again.
|
|
|
|
|
run your code ,you will get the result.
"insert y or Y to repeatY insert y or Y to repeat"
then code end.
it is useless.
fflush(stdout);
you can write
fflush(stdin);
|
|
|
|