如果stat语句不起作用

时间:2012-09-15 16:15:12

标签: c++ xcode

我试图根据用户输入给出if语句为true或false但是当我把条件放入其中时说这个无效的操作数为二进制表达式  ('string'(又名'basic_string')和'int')

这是我的代码

#include <iostream>
#include <string>


using namespace std;

 int main()
 {
 cout << "   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << endl;
 cout << "   X                                             X" << endl;
 cout << "   X                    MENU                     X" << endl;
 cout << "   X                                             X" << endl;
 cout << "   X       Press 1 to Play The Word Game.        X" << endl;
 cout << "   X                                             X" << endl;
 cout << "   X       Press 2 Too See a calculator.         X" << endl;
 cout << "   X                                             X" << endl;
 cout << "   X                                             X" << endl;
 cout << "   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << endl
 << endl;

string userinput;
cin >> userinput;
if (userinput == 1);{

    while (1<2)
    {
        cout << "Enter the Word." << endl;

        string UserInput;
        cin >> UserInput;

        cout << endl << UserInput << " is is not the right word but keep on trying" << endl << endl;

        cout << "Press Enter to Continue.";

        cin.clear();
        cin.ignore(255, '\n');
        cin.get();

        return 0;
    }
}
  if (userinput == 2);
  {   int z;
    int x;
    int sum;
    cout << " welcome to the calculator" << endl << endl;
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();

    cout << "enter your first number";
    cin >> z; cout << endl << endl;
    cout << "enter your second number" << endl;
    cin >> x; cout << endl << endl;
    sum = z + x;
    cout << sum;
 }

cin.clear();
cin.ignore(255, '\n');
cin.get();

return 0;
}

2 个答案:

答案 0 :(得分:2)

  if (userinput == 2);
                    ^^^

同样地,你在if之后到处都有分号,我认为这不是你想要的。

答案 1 :(得分:2)

在此处删除分号:

if (userinput == 1);{

if (userinput == 2);

你无法将字符串与int进行比较。

string userinput;
cin >> userinput;
if (userinput == 1);{

所以试试

int userInput ;

如果您坚持使用字符串,请查看: http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/