2012-09-15 73 views
0

我想有一個if語句是真或根據假以它被賦予的用戶輸入,但是當我把條件,它說這個無效的操作數的二進制表示 (「字符串」(又名這裏的basic_string的')和「廉政」)如果統計陳述沒有工作

是我的代碼

#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
if (userinput == 2); 
        ^^^ 

同樣,你有分號的if我想這是不是你想要的東西后,隨處可見。

+0

沒有幫助很多還在說同樣的事情 – user1610489

+0

@ user1610489看看我的回答 –

+0

@ user1610489是,將其更改爲'如果你,如果你想比較字符串不能使用== int'。但是,你的目的是使用整數。 –

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/

+0

非常感謝這已經摺騰我了很長一段時間 – user1610489

+0

@ user1610489如果它幫助你能接受的答案,這也將意味着人們可能會回答你的問題。 –

+0

的downvoter能否解釋一下它到底我做錯了什麼? –