Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

if statements doesnt work as I want them to

This topic is 4954 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

From my main menu you have the option to change the speed of the game. This is the code I use do to do it:
		case 53:
			system("cls");
			cout << "Current gamespeed: " << gameSpeed << endl;
			while(1) {
                cout << "Input gamespeed (lower value = faster game, default = 250): ";
			        cin >> gameSpeed;
			        if(gameSpeed < 1 || gameSpeed > 250)
                        cout << "\nERROR: Value out of bounds, speed must be between 1 and 250.";
                    
                    if(gameSpeed > 0 || gameSpeed < 251)    // gamespeed ok
                        break;
            }                                     
            
			cout << "\n\nGamespeed set. Press any key.";
			getch();
			system("cls");
			break;


If I input a value thats out of bounds (below 1 or above 250) it prints the error message but then goes on and breaks the loop anyways. I dont know whats wrong. Must be something spooky about those if statements Ive made. Anyone see what Im doing wrong? [EDIT] Yikes, that code sure doesnt look good. Whats wrong now? It looks ok in dev c++.

Share this post


Link to post
Share on other sites
Advertisement
You are checking:
gameSpeed > 0 || gameSpeed < 251
This condition is always true. You either want to check:
gameSpeed > 0 && gameSpeed < 251
to check for valid speeds, or
gameSpeed <= 0 || gameSpeed >= 251
to check for invalid speeds.

Share this post


Link to post
Share on other sites
Oh, right. Sorry, didnt see that. Ill look up all those boolean operators (or whatever theyre called, Ill find that out no problem) in MSDN. Thanks! :)

Share this post


Link to post
Share on other sites
You could use an else instead of the second if statement.

Also, align your code correctly:
case 53:
system("cls");
cout << "Current gamespeed: " << gameSpeed << endl;
while(1) {
cout << "Input gamespeed (lower value = faster game, default = 250): ";
cin >> gameSpeed;
if(gameSpeed < 1 || gameSpeed > 250)
cout << "\nERROR: Value out of bounds, speed must be between 1 and 250.";
else
break; // Should this be continue?
}
cout << "\n\nGamespeed set. Press any key.";
getch();
system("cls");
break;

Share this post


Link to post
Share on other sites
The code is alligned in dev c++, I dont know why it ended up like that here. :S

Ill change that second if to else, makes to code easier to read.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!