Archived

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

flukus

Frustrating problem in VC++

Recommended Posts

In my program I basicly have the following if statement: if ("WEST" || "NORTH" || "SOUTH" || "EAST" == uinput) { Lots of other stuff in here } And it works perfectly as long as the user types one of the options. The problem is that if uinput is anything other than one of the options (when I know for certain it has a value) I get an unhandled exception during runtime. Has anyone got any Ideas on whats going wrong? [edited by - flukus on July 30, 2002 8:59:36 AM]

Share this post


Link to post
Share on other sites
i dont know but, try checking uinput == "NORTH" || uinput=="WEST"......and so on.....
and do this:
if(......){
}else{ }

or

if(......){
}else if(....){}

or

use a switch statement,
switch(uinput){
case "WEST":
case "NORTH":.....
default:...... <----if it doesnt match any case.

Share this post


Link to post
Share on other sites
"i dont know but, try checking uinput == "NORTH" || uinput=="WEST"......and so on....."

Thats how I originally had it and neither way works.

"and do this:
if(......){
}else{ }"

I''ve tried it with or without the else and it doesn''t make a difference, the program doesn''t get to the else.

"is ''uinput'' a string or some sort?"

uinput is a CString.

I''ve also tried substituting uinput with a const CString to no avail.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It will never get to an else because your if statement is flawed. It should be:

if ("WEST" == uinput || "NORTH" == uinput || "SOUTH" == uinput || "EAST" == uinput)
{
}
else


The one above will actually get to an else. The one you had before will always be true.

Share this post


Link to post
Share on other sites
First, the way you have the if statement in the original post, it will always evaluate to true. "WEST" is going to evaluate to a memory address, which will be non-zero, meaning true, and the rest of the if never gets evaluated (since you used the || operator, the first true one it finds means the whole statement is true).

The other problem is that you put uinput on the right side of the operator ==. Since the left side is a char*, the right side gets converted to a char* (a valid CString operation), so you''re comparing different pointers, which is always going to be false. If you want to use the CString operator==, then the CString has to be on the left side. So if you did:

if("WEST" == uinput || "NORTH" == uinput)

It would never evaluate to true, though it LOOKS correct.

The best way is what _rpg_guy said, since you could put a final else that handles bad input. Or you could do this:

if(uinput == "WEST" || uinput == "NORTH" || etc...)

~~~~~~
http://www.goatsoft.com
Try my Tri-Towers clone

Share this post


Link to post
Share on other sites
Hmm, forget that part about the left/right, apparently they define an == for the char* on the left. My bad for not looking that up first.

You may have a problem depending on how you get the input, of a trailing newline character in the uinput, that''s causing the == comparison to fail.

~~~~~~
http://www.goatsoft.com
Try my Tri-Towers clone

Share this post


Link to post
Share on other sites
Ahhh, it works perfectly like this:
if (uinput == "WEST" || uinput == "NORTH" || uinput == "SOUTH" || uinput == "EAST")

"if (uinput.CompareNoCase("SOUTH") == 0)
{
// stuff
}"

Theres a 20+ line block of code in the if brackets so I didn''t want to do it that way and I''d done uinput.MakeUpper befor it started.

"You may have a problem depending on how you get the input, of a trailing newline character in the uinput, that''s causing the == comparison to fail. "

I''m using a normal edit box so I''m pretty sure that can''t happen.

Thanks alot to everyone that helped!

One last Q though, how do you declare a knew line in an editbox?

Share this post


Link to post
Share on other sites