J
Jim Bancroft
I'm having trouble reading input from the keyboard. My goal: to prompt the
user for some text and then react accordingly. Here's what I've got so far
(relevant parts excerpted):
char input[2];
while(1)
{
printf("Type 0, 1 or 2 to do something. Press 3 to quit\n");
fgets(input,2,stdin);
switch(input[0])
{
case '0' : printf("you typed 0\n");break;
case '1' : printf("you typed 1\n");break;
case '2': printf("you typed 2\n");break;
case '3' : printf("you just quit\n");
default : printf("I don't know what you just typed\n");
}
}
That code works ok, but for some reason every time I type something the loop
runs twice-- I see the expected reaction, followed by the default statement.
So for instance, if I type "1" it tells me I typed a 1, but then it loops
and tells me it doesn't know what I typed.
I'm new to reading the keyboard in C, so any help you can give I'd be glad
for. Essentially I'd like to read a character and newline, then react
accordingly. Thanks!
}
user for some text and then react accordingly. Here's what I've got so far
(relevant parts excerpted):
char input[2];
while(1)
{
printf("Type 0, 1 or 2 to do something. Press 3 to quit\n");
fgets(input,2,stdin);
switch(input[0])
{
case '0' : printf("you typed 0\n");break;
case '1' : printf("you typed 1\n");break;
case '2': printf("you typed 2\n");break;
case '3' : printf("you just quit\n");
default : printf("I don't know what you just typed\n");
}
}
That code works ok, but for some reason every time I type something the loop
runs twice-- I see the expected reaction, followed by the default statement.
So for instance, if I type "1" it tells me I typed a 1, but then it loops
and tells me it doesn't know what I typed.
I'm new to reading the keyboard in C, so any help you can give I'd be glad
for. Essentially I'd like to read a character and newline, then react
accordingly. Thanks!
}