Hi,
I've been having some problems with the getchar() statement. When I put it in a loop (to make an interactive input), I can't get user input anymore from within the loop.
Even the following simple program doesn't work:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char response, text[255];
int num;
do
{
printf("Enter a number: ");
fflush(stdout);
fgets(text, 255, stdin);
num = (int)strtol(text, NULL, 10);
printf("You entered the number: %d\n", num);
fflush(stdout);
printf("\nRun again? ");
fflush(stdout);
response = getchar();
} while (response != 'n');
return EXIT_SUCCESS;
}
(On my computer, I had to place the fflush() statements, otherwise I wouldn't see the output).
The program above allows me to enter only ONE number. After that, it just says I entered 0.
What's going on and how can I solve this problem?