I have my code and it seems to run fine but when I run the code and enter left or right it uses the else statement instead of the if statement(s).
If I remove the else statement the program still runs but now if I enter left or right no text appears in the console.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
char word;
cout << "you came to a split in the road." << endl;
cout << "Do you go left or right?" << endl;
cout << "Please enter left or right." << endl;
cin >> word;
if (word == 'right' || word == 'Right') {
cout << "You went right." << endl;
if (word == 'left' || word == 'Left') {
cout << "You went left." << endl;
}
}
else {
cout << "You didn't enter left or right...\n Please try again.";
}
return 0;
}
I've tried using an array with my char
char word[80];
But when I do I get an error at my if statements that says "error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|"
I've had no luck finding a solution on google and it's driving me insane.
It goes back to the else statement, even if I enter left or right.
"right" should work.
But the code for "left" is nested inside the code for "right" so it can never be executed. If the user entered right, it definitely isn't left, and if they enter left, it does indeed go straight to the else.
Seriously, time to start looking at your own code, reading the warnings and errors, and thinking for yourself. You're now beyond syntax errors and into the realm of simply writing wrong code.
Look, I'm still learning how to program. I haven't even made it past the console yet.
I'm sorry that I thought a nested if statement should work, but I am in the beginners forum so I can get help for my beginners mistakes so I can learn and progress and hopefully not need help later.
Thank you to everyone that helped me fix my code!!! :D