通过比较char来设置字符串的麻烦

时间:2018-04-13 03:01:16

标签: c++ c++11

我正在尝试从用户那里获得char输入,然后根据键入的字符设置字符串。我做错了什么?

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char direction;
    string printDirection;

    cout << "Inbound or Outbound? (I or O)" << endl;
    cin >> direction;
    if (direction == 'o' || 'O'){
        printDirection = "Outbound";
    }
    else if(direction == 'i' || 'I'){
        printDirection = "Inbound";
    }
    else{
        cout << "Error";
        return 1;
    }
    printDirection;

    return 0;
}

1 个答案:

答案 0 :(得分:1)

if (direction == 'o' || 'O')

应该是

if (direction == 'o' || direction == 'O')

和其他if语句中的相同内容。