Frustrating problem in VC++

Started by
8 comments, last by flukus 21 years, 8 months ago
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]
Advertisement
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.
is ''uinput'' a string or some sort? How are you comparing it to a string literal?
Switch won''t work here...
<a href="http://www.purplenose.com>purplenose.com
"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.
if (uinput.CompareNoCase("SOUTH") == 0)
{
// stuff
}
else
if (uinput.CompareNoCase("WEST") == 0)
{
// Code for west
}
...and so forth
What do you want to do when the user enter something unexpected??
Wouldn''t it be a good idea to use a try-catch block??
Redefining the problem... is not SOLVING the problem!
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.
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
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
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?

This topic is closed to new replies.

Advertisement