- I have a if statement. Inside I have a while loop. I've noticed that in the first iteration gets() recieves something without me typing anything(I think it's the previous \n from getchar()). In the second iteration though it works properly. But if I use scanf() it works the first time. WHY?! I need to use gets() so this is really ........ing me off!
- Also the the getchar() inside the while gets ignored. If I put two getchar() together it works. It also works if I put a getch(). WHY!?
Here's my code:
Code:
int bonusRound1()
{
char y_or_n, firstAns[50], ans[50], exit[]="exit", quit[]="quit", yes[]="y", no[] = "n", nl;
int i, bonusPoints=0, correctAns=0;
char charlie[] = "charlie";
printf("\n Would you like to play a bonus round? y or n? ");
y_or_n=getchar();
if(y_or_n == 'y')
{
while(correctAns != 1)
{
printf("\n________________________________________________________________________________\n");
printf(" Bonus question 1. Who's quote is this?\n");
printf("\n \"It's French, the French are coming! I've never been so happy to hear the\n"
" French.\"\n");
printf("\n > ");
//gets(firstAns);
scanf(" %s", firstAns);
for(i=0; i<=strlen(firstAns); i++)
{
ans[i] = tolower(firstAns[i]);
}
if(strcmp(ans, charlie) == 0)
{
printf("\n That's correct! Press enter to continue...\n");
//getchar();
//getchar();
getch();
bonusPoints += 10;
correctAns=1;
}
else if(strcmp(ans, quit) == 0 || strcmp(ans, exit) == 0)
{
break;
}
else
{
printf("\n That's incorrect! Press enter to continue...");
//getchar();
//getchar();
getch();
continue;
}
}
}
if(y_or_n == 'n')
{
return;
}
}