Just learning C programming, and stuck on what I'm sure is something trivial about Do/While loops. I have a block of code that requires the user to hit 'E' to exit the program:
char exitletter;
do {
printf ("Please hit E to exit the Program\n");
exitletter = getchar();
} while (exitletter !='E');
However, if the user enters an incorrect character, it prints "Please hit E to exit the Program" twice. If the user enters say abcd, it prints the message five times.
Can someone please explain what is happening here?
A different answer though late:
You could have called fflush
right after reading the character.
char exitletter;
do {
printf ("Please hit E to exit the Program\n");
exitletter = getchar();
fflush(stdin);
}
while (exitletter !='E');
and this would have worked like you expected it to.