Archived

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

Using char variables as test parameters in while loops

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

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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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" {
;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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'')

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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;
}

Share this post


Link to post
Share on other sites

std::string usermark
while(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.

Share this post


Link to post
Share on other sites
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''

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

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;
}


This is rediculus 9 posts for such simple question, bah...

Share this post


Link to post
Share on other sites