I need help with code!!

here is my code for an rpg text game:

cin >> input >> input2;
while ( input == "go" )
{
if (input2 == "north" or "North" or "n" or "N") {
cout << "north" << endl;
break;
}else{
cout << "Command " << input << input2 << " is not found!" << endl;
}

break;
}


It doesnt seem to be working, no matter what i type in after go, it prints "north";
Found the problem, kind of.

I rewrote your code to this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
std::string input, input2;
std::cin >> input >> input2;
if ( input == "go" )
{
if (input2 == "north")
{
std::cout << "north" << std::endl;
}else{
std::cout << "Command " << input << input2 << " is not found!" << std::endl;
}
}
}

and it started working. My guess is it's something to do with a buffer of the breaks, but to be honest. I really don't know.

Maybe someone more experienced can evaluate further on this.
thanks ill try it out!
Hi,

What you have is (in pseudo code)

if(expression or expression or expression or expression)
Now the compiler evaluates each expression to determine of it is conceptually either true or false. What does "North" evaluate to? The answer is true. Now you have or relational operators - which means that if any of the expressions are true, the whole statement is true. So an answer is :

if (input2 == "north" || input2 == "North")

But this approach is really bad, what if the user puts in nOrth ? Now you have to account for every permutation involving upper and lower case. This why it is best to ask for just one char as the input, convert that to upper or lower case with the toupper or tolower functions, that way you won't have logic for N as well as n, just N say.

You can also use a switch statement - investigate that.

Hope all goes well :+)
also one more thing, how can i account if input2 is empty?
like "go" then it spits out, "sorry you must supply a direction"
how can this be accomplished?
Hi,

You can put an else condition, or if using a switch, a default: case.

As I said, don't get whole words as input, just 1 char.
Topic archived. No new replies allowed.