Trouble with code...

Started by
15 comments, last by Jimmyp4ge 21 years, 5 months ago
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"); } }
Advertisement
scanf expects a pointer to the target variable

= is an assignment, == is a test.

( i = 1 ) evaluates to 1, which is true
( i = 2 ) evaluates to 2, which is true

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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]
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");
}
}
}
Apostrophes on the 1 and 2 in the while statement. You can find a bunch of wonderful examples like this on http://www.cplusplus.com
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
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
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
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");
}

}

}
}
Recall fruny''s advice about pointers and scanf.

-D

This topic is closed to new replies.

Advertisement