为什么代码不能在c ++中正常运行

时间:2017-11-12 22:29:20

标签: c++

int main()
{

    std::cout << " Here is my calculator!\n ";
    int input;
    std::cout << " What operation do you wish to use? Use +, -, x, / as signs!\n ";
    std::cin >> input;
    std::cin.ignore();
    if ( input == "+" )
 {
    int no1;
    std::cout << " Okay, addition what is your first number? ";
    std::cin >> no1;
    std::cin.ignore();
    std::cout << " Okay, So your first number is " << no1 << "!\n";
    int no2;
    std::cout << " So you're first number is " << no1 << " What do you wish you're second number to be? " << "!\n";
    std::cin >> no2;
    std::cin.ignore();
    std::cout << " Okay so you're second number is " << no2 << "In the end the equation is " << no1 << "+" << no2 << "!\n";
    std::cout << " The answer to you're question is " << no1 << "+" << no2 << "=" << no2 + no1 << "!\n";
    std::cout << " Thank you for using my calculator, Bye!\n";

}  

跑步者说第9行有一个问题,一个是if语句。任何帮助是极大的赞赏。提前谢谢

1 个答案:

答案 0 :(得分:2)

在第5行,您将输入声明为int。在第9行,您将输入(整数)与string进行比较,这是不可能的。如果您要将"+"更改为'+',则检查会有效。 将输入更改为char并将条件更改为if (input == '+')会更好(如DimChtz所述)。