Graham E.
Graham E.

Reputation: 47

Could anyone help me with if statements and strings in C++?

I am having a bit of trouble with if statements and strings/characters in c++. Here is my code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "-----------------------------" << endl;
    cout << "|Welcome to Castle Clashers!|" << endl;
    cout << "-----------------------------" << endl;

    cout << "Would you like to start?" << endl;

    string input;

    cout << "A. Yes ";
    cout << "B. No " << endl;
    cin >> input;
    if(input == "a" || "A"){
        cout << "Yes" << endl;
    }else{
        if(input == 'b' || 'B'){
            return 0;
        }
    }
    return 0;
}

At my if statement it checks if the string input is equal to yes, and if it is not it should go to the else statement. This is where the trouble began, once I ran my program in the console when I type anything besides "a" or "A" it still says yes. I've tried doing it with chars/characters but I get the same output. Could anyone assist me?

Upvotes: 0

Views: 124

Answers (3)

Bill
Bill

Reputation: 400

C didn't have "real" boolean values - instead, anything that equals 0 is considered false, and anything different from that is considered true. While C++ introduced a bool type, it still maintains the old C behavior for compatibility reasons.

As Cornstalk said, your (input == "a" || "A") is the same as ((input == "a") || ("A")), and "A" != 0, so it always evaluates to true - that's why it'll always enter into that if block. What you want is:

if (input == "a" || input == "A")

The same holds true to the next statement (comparing it to 'B'), but there's one extra problem in there: You're using single quotes ( ' ) instead of double quotes ( " ), which makes it a char instead of a string. To make both variables the same type, just use double quotes, and it'll end up like this:

if(input == "b" || input == "B")

Upvotes: 2

MikeCAT
MikeCAT

Reputation: 75062

"A" and 'B' will always be true in typical implementation.

You should also compare input against them.

Also compareing std::string with char doesn't seem supported, so you should also use string literals for b and B.

Try this:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "-----------------------------" << endl;
    cout << "|Welcome to Castle Clashers!|" << endl;
    cout << "-----------------------------" << endl;

    cout << "Would you like to start?" << endl;

    string input;

    cout << "A. Yes ";
    cout << "B. No " << endl;
    cin >> input;
    if(input == "a" || input == "A"){
        cout << "Yes" << endl;
    }else{
        if(input == "b" || input == "B"){
            return 0;
        }
    }
    return 0;
}

Upvotes: 3

Cornstalks
Cornstalks

Reputation: 38218

It should be input == "a" || input == "A". You have to test each case individually. Right now your code is equivalent to (input == "a") || "A", which evaluates to true because "A" decays to a non-zero pointer.

Upvotes: 6

Related Questions