kboy
August 3rd, 2010, 07:09 PM
Hello, I'm trying to create a simple program which asks a number of people a yes/no question. when i enter (n for example) as the answer the while loop continues accordingly, but in the second iteration it doesn't request any input from the user, but automatically inputs 'a new line' char to the (input) variable.
as i see it, on every iteration it should receive a new char input from the user using scanf("%c", &input); , but it doesn't.
#include <stdio.h>
#define YES 'y'
#define NO 'n'
#define FINISH 'f'
int main(void)
{
char input = YES;
int doThink, dontThink, total;
doThink = dontThink = 0;
while (input != FINISH){
printf("Is there life outside earth? ");
scanf("%c", &input);
if ((input != YES) && (input != NO))
printf ("Input incorrect, please press (%c) for yes (%c) for, or (%c) for finish:\n\n", YES, NO, FINISH);
else if (input == YES)
doThink++;
else if (input == NO)
dontThink++;
}
return 0;
}
please help.
as i see it, on every iteration it should receive a new char input from the user using scanf("%c", &input); , but it doesn't.
#include <stdio.h>
#define YES 'y'
#define NO 'n'
#define FINISH 'f'
int main(void)
{
char input = YES;
int doThink, dontThink, total;
doThink = dontThink = 0;
while (input != FINISH){
printf("Is there life outside earth? ");
scanf("%c", &input);
if ((input != YES) && (input != NO))
printf ("Input incorrect, please press (%c) for yes (%c) for, or (%c) for finish:\n\n", YES, NO, FINISH);
else if (input == YES)
doThink++;
else if (input == NO)
dontThink++;
}
return 0;
}
please help.