Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ While Loop

Started by Jettoz Dec 7, 2006 at 10:07 PM 10 replies 1.6k views
Original Post
Jettoz
Jettoz
Hello, I'm trying to figure out why this While loop runs.

#include <iostream>
#include <stdlib.h>
#include <string>

#include "Mage.h"
#include "Warrior.h"

// Globals

// -----------------

int Health;
int Defence;
int Attack;

// -----------------

using namespace std;

int main()
{
	// Title
	cout << "********************\n     Text Game\n********************\n\n";
	system ("pause");
	cout << "*********\n   Menu\n*********\n\n1) Start\n2) Quit\n\n";
	
	// Check Menu Input
	string MenuCheck = "";
	cin >> MenuCheck;

	while (MenuCheck != "1" || "2")
	{
		if (MenuCheck == "1")
		{
			cout << "Starting Game ...\n";
			system ("pause");
			return 0;
		}
		else if (MenuCheck == "2")
		{
			return 0;
		}
		else
		{
			cout << "Invaild Input!  Try Again.\n";
			cin >> MenuCheck;
		}
	}

	return 0;
}

// Game Start
void StartGame()
{
	system ("cls");
	cout << "STARTING GAME ...";
	system ("pause");
}



If I type 1 or 2 it still goes through the while loop, I have no idea why? What I'm trying to do is check for input of 1 or 2, I have used char as well for this. If it doesn't equal 1 or 2 repeat loop and ask for input. I would use a switch if I knew how after checking case '1' (if you use a char) and case '2' then at default: have it ask for input then loop the switch until either 1 or 2 was typed. What should happen is if 1 or 2 is pressed the program should quit. Please ignore my other code, just look at the while (). I have code I still need to add.
________VB/C++/C# Programmer
King Mir
King Mir
You need to do while (MenuCheck != "1" || MenuCheck != "2")

As is your compiler is doing this:
1) check if MenuCheck != "1" is true (=0)
2) check if "2" is true. Literal strings are never equal to 0, so this till always return true.
3) evealuate first value || second value. since the second value is always true, this allway evaluates to true.

Jettoz
Jettoz
Oh I forgot about that! I made that same mistake while learning C#. It still runs through the loop even when I press 1.

If anyone can answer my question on how to repeat switchs that would be great as well.
________VB/C++/C# Programmer
CadetUmfer
CadetUmfer
string MenuCheck = "";while(1) {    cin >> MenuCheck;    if (MenuCheck == "1") {        cout << "Starting Game ...\n";        system ("pause");        return 0;    } else if (MenuCheck == "2") {        return 0;    } else {        cout << "Invaild Input!  Try Again.\n";    }}
Anthony Umfer
Jettoz
Jettoz
Thanks, I'll make my changes and post back!

EDIT: It still goes through the loop, I even did a do while loop.
________VB/C++/C# Programmer
jouley
jouley
You're on the right track, but I suggest you read up on the operator precedence used in C(regular, plusplus, most languages, really). The condition in the loop doesn't do what you think it does. You read the line out loud to yourself like this: "As long as MenuCheck is not equal to '1' or '2', execute the following code." You and the compiler don't see eye to eye ;) Here's why: First, it evaluates MenuCheck != "1". However, the result of this comparison has absolutely nothing to do with the outcome of the final result since you followed it with || "2". Once the program encounters ||, it evalutes the right side, which is a constant, "2". It casts the ASCII value of 2 into a boolean, and, as it is nonzero, is true, OR's it with the result of the left side and comes up with a final result of true, causing your loop to always execute. The following is an example of a working while( ) loop, taking into account operator precendence:
while (MenuCheck != "1" && MenuCheck != "2")


Best of luck!

-jouley
Jettoz
Jettoz
Wow, thanks! Now I understand what I did wrong.
________VB/C++/C# Programmer
MrAccident
MrAccident
Quote:
Original post by jouleyOnce the program encounters ||, it evalutes the right side, which is a constant, "2". It casts the ASCII value of 2 into a boolean, and, as it is nonzero, is true

Just a minor nitpick. [wink]

It's not actually casting the contents of the string "2" as a boolean; it's using the address of the string "2", which will point to somewhere in the program's data region. This address will definitely not be zero, so its boolean value is true. You could use the string literal "\0" as a boolean, and it would still evaluate as true, because though the string contains the null character, it is located at a non-zero address, and the address is what is being evaluated.
Emmanuel Deloget
Emmanuel Deloget
Quote:
Original post by CadetUmfer
*** Source Snippet Removed ***


The dreaded infinite loop that is not infinite construction! [smile].

When you can avoid such construction, avoid it. It has more problems that it seems - it's quite fragile (ie. may suffer greatly from minor modifications), not that easy to read ("where the hell is this break/return/goto?"), and is usually a Bad Way to reproduce the behavior of the goto keyword anyway.

It is still far better to use a correct condition in loops [smile]
Jettoz
Jettoz
Thanks, I'm sorry I posted some what un-finished code with some things I really should have fixed before, here is the new code working fine. Sorry about that.

#include <iostream>#include <stdlib.h>#include <string>#include "Mage.h"#include "Warrior.h"// Globals// -----------------int Health;int Defence;int Attack;// -----------------using namespace std;int main(){	// Title	cout << "********************\n     Text Game\n********************\n\n";	system ("pause");	cout << "*********\n   Menu\n*********\n\n1) Start\n2) Quit\n\n";		// Check Menu Input	string MenuCheck = "";	cin >> MenuCheck;	if (MenuCheck == "1")	{		cout << "Starting Game ...\n";		system ("pause");		// Start Game TO DO	}		else if (MenuCheck == "2")	{		return 0;	}		while (MenuCheck != "1" && MenuCheck != "2")	{		cout << "Invaild Input!  Try Again.\n";		cin >> MenuCheck;		if (MenuCheck == "1")		{			cout << "Starting Game ...\n";			system ("pause");			// Start Game TO DO		}			else if (MenuCheck == "2")		{			return 0;		}	}	return 0;}


(I have header files but there is no need to show them because they have little to do with my loop)

I've almost finished reading two C++ books today which I started last night so I understand a lot more now, thanks again! Learning my third language is pretty easy for how much time I've spent and for how much I've learned.

[Edited by - Jettoz on December 8, 2006 4:44:58 AM]
________VB/C++/C# Programmer
kristian012
kristian012
Just wondering: why are you using "||" instead of "&&". Won't it always evaluate to true since MenuCheck will always be either different from 1 or different from 2 (since "1" != "2")?
Jettoz
Jettoz
It's no problem, I pasted my old code by mistake I’ll change that. It's 3:15am and I haven’t slept in 29 hours so I'm not thinking to straight, so much paper work to-do! :(
________VB/C++/C# Programmer

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.