Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Jimmyp4ge

Trouble with code...

This topic is 5751 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This code seems simple enough.. I am having trouble when entering anything to this function.. It could be this function (even though I see nothing wrong with it!). Or it could be what it''s going to? Can someone help.. I have the debug info if you would like.. void myloop(void) { int i = 0; while(i != 1 || i != 2) { printf("\nHi, welcome..\n\n"); printf("1.) Enter Game\n"); printf("2.) Exit Game\n"); printf("Your choice[1 or 2]: "); scanf("%d",i); if(i = 1) { gameloop(); } if(i = 2) { return; } printf("Try again..\n"); } }

Share this post


Link to post
Share on other sites
Advertisement
Ok.. i changed the == part in the if's...
so the problem is with scanf?


[edited by - jimmyp4ge on October 22, 2002 11:52:45 PM]

Share this post


Link to post
Share on other sites
I changed the code to look like this... but it still wont work for me...

void myloop(void)
{
int i;

while(i != 1 || i != 2)
{
printf("\nHi, welcome..\n\n");
printf("1.) Enter Game\n");
printf("2.) Exit Game\n");
printf("Your choice[1 or 2]: ");
scanf("%d",&i);
switch (i)
{
case 1:
{
gameloop();
}
case 2:
{
return;
}
printf("Try again..\n");
}
}
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Look at the truth table for ''i != 1 || i != 2''. If i is equal to 1, then the statement is true for then it is not equal to 2. Contrariwise, if i is equal to 2 then the statement is true because i is not equal to 1. If it is equal to neither 1 nor 2, then the statement is doubly true.

Your switch statement also has a problem. A ''break;'' statement must seperate each clause or the program will execute every clause.

Also, you should use just one method for your loop/break mechanism. Either make the loop conditional upon a 2 -- while (i != 2){} -- in which case do nothing in the case of a 2 being entered for it will automatically break out of the loop that iteration. Or make it an explicit infinite loop so that the reader expects a break or return in the middle of the loop -- while (true){} .

-D

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
One other thing I just noticed. Before your ''try again'' printf statement, you need to explicitly notify the compiler that it is the default case by typing ''default:'' just before it.

-D

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Melraidin, as much as I dislike causing turmoil, I feel that you may be mistaken in this case. Since Jimmyp4ge is using the ''%d'' field specifier in their scanf statement, the data is translated to an integer type before being stored in i. Therefore it is necessary to check for numbers rather than symbols.

Jimmyp4ge, you initialized i to 0 in your first code fragment which was good. However, you forgot to initialize it in your second. Be sure that the code you are trying to run initializes i. Otherwise undefined behavior may result.

-D

Share this post


Link to post
Share on other sites
Hey, you totally blew my mind... yea, that truth table didn''t work.. buuuutt... still havin trouble.. this thing is crazy.. it looks so simple!

void myloop(void)
{
int i = 0;

while(true)
{
printf("\nHi, welcome..\n\n");
printf("1.) Enter Game\n");
printf("2.) Exit Game\n");
printf("Your choice[1 or 2]: ");
scanf("%d",i);
switch (i)
{
case 1:
{
gameloop();
break;
}

case 2:
{
return;
break;
}

default:
{
printf("Try again..\n");
}

}

}
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Recall fruny''s advice about pointers and scanf.

-D

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!