2017-10-18 76 views
-3

我正在建立一個簡單的菜單,用戶可以從中選擇。出於某種原因,||不工作如果其他C++

else if (letterSelect == "C" || letterSelect = "c") 

的||正在出現一個錯誤。另一個||工作正常。錯誤是:沒有運算符「||」匹配這些操作數。操作數類型是; bool ||的std :: string

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

int main() 
{ 


cout << "********* General Purpose Calculator Program ***************\n" 
    << "Please Select an Option by inputting its associated letter\n" 
    << "A) Determine Highest of Three Numbers\n" 
    << "B) Reverse a Number\n" 
    << "C)Calculate Parking Garage Charges\n" 
    << "D) Exit\n"; 

string letterSelect = " "; 


cin >> letterSelect; 

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

{ 

    //call program listed for A; 

} 

else if (letterSelect == "B" || letterSelect == "b") 

{ 

    //call program listed for B; 

} 

else if (letterSelect == "C" || letterSelect = "c") 

{ 

    //call program listed for C; 

} 

else (letterSelect == "D" || letterSelect == "d"); 


    cout << "Exiting..."; 


} 
+0

letterSelect =「c」應該是letterSelect ==「c」 –

回答

1

這裏:

else if (letterSelect == "C" || letterSelect = "c") 

您在條件第二部分失蹤=。 它應該是letterSelect == "c"

1

只是一個錯字。 '||'的右側,在你的代碼中是一個賦值(=),而不是一個相等性檢查(==)。

1

的代碼

letterSelect = "c" 

線路的分配操作,不用比較。更改爲

letterSelect == "c" 

它應該工作。

快樂編碼。