Using char variables as test parameters in while loops

Started by
9 comments, last by Sir_Spritely 20 years, 9 months ago
In a nutshell, I cannot understand why the following does not work. It makes complete sense to me but then again I am a fruitcake

char userMark[1];
char comMark[1];
while ( userMark != "0" || userMark != "X" || userMark != "x" )
     {
	cout << "Please choose your preferred mark a 0 or an X:";
	cin >> userMark;
      }
What I am saying is that until the user enters either a 0, X or x then to continue looping through this. However when the user enters the 0, X or x the loop is not broken. Anyone know why? I have done a search on google but cannot find the answer all I get is a load of stuff on how to declare variables. By the way, I know I am creating a string which only takes in one character, which seems pointless but the one letter might change later down the line which is why I did some forward planning and used a string to hold this. Help appreciated, Paul.[/source]
Advertisement
Looks like your logic is slightly incorrect. You want it to loop until a user enters a 0, x or X right?

while (userMark != "0" && userMark != "X" && userMark != "x" {
;
}
instead of "0", write ''0'' etc. "0" evaluates to const char*

____________________ ____ ___ __ _
Enselic''s Corner - My site. Go test my game Spatra and see if you can beat it onto the Official Spatra Top 10. (source available)
CodeSampler.com - Great site with source for specific tasks in DirectX and OpenGL.
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Hey,

Still does not work changing the || for the &&. I get some nice errors when trying to use the '' instead of the " as well.

Anyone help?
It doesn''t look like you initialize your userMark variable (or array, rather). Try a do-while loop and change userMark to a regular char.

char userMark;do {  cin >> userMark;} while (userMark != ''0'' && userMark != ''X'' && userMark != ''x'') 
try dropping the [1] too. You can''t use != on strings or arrays
char userMark;char comMark;while ( userMark != ''0'' && userMark != ''X'' && userMark != ''x'' ){	cout << "Please choose your preferred mark a 0 or an X:";	cin >> userMark;}
std::string usermarkwhile(userMark!="0" && userMark!="X" && userMark !="x")


You are trying to compare a pointers in your original example and that will not work. In addition the character array will owerflow, if you type more than one character. So you should always use std::string when reading characters using cin.

userMark[0]!=''0'' && userMark[0]!=''X'' && userMark[0]!=''x'' would compile but there''s the owerflow problem.
Anonymous poster has a good point. Your logic is incorrect. (Replace all || with &&) And you need to initialize your char array before you enter the loop.
This has already been answered, but to clearify...

userMark is an address (a pointer to an array).
"0" is an address (a pointer to a string).

userMark=="0" is a-ok by the compiler, as they''re both pointers, but logically incorrect here as pointed out.

''0'' is not an address, it''s a character. That''s why you get an error with userMark==''0''
Use a normal char instead, then compare with the '.
If you use the array of one, you'll have to check the first([0]) element.

You CANNOT check char pointers (which arrays ARE, so..)!!

[edited by - Pipo DeClown on July 7, 2003 8:29:59 AM]

This topic is closed to new replies.

Advertisement