Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C] Nested if/else help

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Thu Oct 28, 2010 5:36 pm    Post subject: [C] Nested if/else help Reply with quote

Hi, I'm having trouble figuring out what's wrong with my code. At first, I figured that it was something to do with
Code:
char answer
, 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
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Oct 28, 2010 6:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Thu Oct 28, 2010 7:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 891

PostPosted: Thu Oct 28, 2010 10:02 pm    Post subject: Reply with quote

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
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Fri Oct 29, 2010 5:07 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites