View previous topic :: View next topic |
Author |
Message |
407 Master Cheater
Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Thu Oct 28, 2010 5:36 pm Post subject: [C] Nested if/else help |
|
|
Hi, I'm having trouble figuring out what's wrong with my code. At first, I figured that it was something to do with , so I made 3 more chars for each question.
When debugging, it prints
Code: | printf("\nDid you mess with it? (y/n): ");
|
then jumps to and prints
Code: | printf("\nCan you be blamed? (y/n): "); |
without stopping for the scanf.
Any suggestions?
Here's the code I'm working on
Code: | void main()
{
char answer;
char answer2;
char answer3;
char answer4;
printf("Does it work? (y/n): ");
scanf("%c", &answer);
if(answer == 'y' || answer == 'Y')
{
printf("Don't mess with it.");
}
else
{
printf("\nDid you mess with it? (y/n): ");
scanf("%c", &answer2);
if(answer2 == 'y' || answer2 == 'Y')
{
printf("\nYou idiot.\n");
printf("\nDoes anyone know? (y/n): ");
scanf("%c", &answer3);
if(answer3 == 'y' || answer3 == 'Y')
{
printf("\nYou poor idiot.");
}
else
{
printf("\nHide it.");
}
}
else
{
printf("\nCan you be blamed? (y/n): ");
scanf("%c", &answer4);
if(answer4 == 'y' || answer4 == 'Y')
{
printf("\nYou poor schmuck.");
}
else
{
printf("\nIgnore it.");
}
}
}
getchar();
getchar();
return;
} |
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Oct 28, 2010 6:06 pm Post subject: |
|
|
i'm too lazy to look at the code right now but you probably have an extra character floating around in the input stream depending on what you enter.
i guess you could use fgets and read stdin instead.
|
|
Back to top |
|
 |
407 Master Cheater
Reputation: 0
Joined: 25 Oct 2007 Posts: 357
|
Posted: Thu Oct 28, 2010 7:23 pm Post subject: |
|
|
slovach wrote: | i'm too lazy to look at the code right now but you probably have an extra character floating around in the input stream depending on what you enter.
i guess you could use fgets and read stdin instead. | Ah, it appears that enter is also a character, and when I hit enter it gets stored in..
I'm doing this as homework, and I'm not allowed to use anything else lol
|
|
Back to top |
|
 |
justa_dude Grandmaster Cheater
Reputation: 23
Joined: 29 Jun 2010 Posts: 891
|
Posted: Thu Oct 28, 2010 10:02 pm Post subject: |
|
|
Try using fflush(stdin) after each read - depending on your compiler, it will probably work. Explain why you're doing so and admit as much in a comment for the teacher, as well as a curse for forcing you to use scanf - her curriculum sucks.
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Oct 29, 2010 5:07 pm Post subject: |
|
|
justa_dude wrote: | Try using fflush(stdin) after each read - depending on your compiler, it will probably work. Explain why you're doing so and admit as much in a comment for the teacher, as well as a curse for forcing you to use scanf - her curriculum sucks. |
this is undefined behavior, it's only meant for output according to the c standard.
even if some compilers have decided to implement it, probably not a good idea to rely on
|
|
Back to top |
|
 |
|