2015-09-29 34 views
0

我想要製作一個基本的計算器,它將使用一個do-while循環並提示用戶是否希望從頭開始重新運行計算器。當使用cin時字符串的右側操作數錯誤

我在cinyesno字符串字面解釋遇到以下錯誤:

<Error C2679 binary '>>': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)> 

我怎樣才能解決這個問題?

int main() 
{ 
    double x; 
    double z; 
    char o; 
    string a; 
    char Y, y; 
    do 
    { 
     cout << "Please input a value for x: " << endl; 
     cin >> x; 
     cout << "Please input a value for z: " << endl; 
     cin >> z; 
     cout << "Please pick an operation to do: */+ -" << endl; 
     cin >> o; 

     switch (o) { 
      case '+': 
       cout << x << " + " << z << " = " << x + z << endl; 
       break; 
      case '-': 
       cout << x << " - " << z << " = " << x - z << endl; 
       break; 
      case '*': 
       cout << x << " * " << z << " = " << x*z << endl; 
       break; 
      case '/': 
       if (z != 0) 
       { 
        x/z; 
        cout << x << "/" << z << " = " << x/z << endl; 
       } 
       else 
       { 
        cout << "Can not divide by zero! Nice try, Pedersen!" << endl; 
       } 
       break; 
      default: 
       cout << "/n/n/tThank you for using my calculator!" << endl << endl; 
     } 
     system("cls"); 
     cout << "/n/n/tDid you want to run the calculator again?" << endl << endl; 
     cin >> a; 
    }while (a == "Yes" || a == "yes"); 
    system("pause"); 
    return 0; 
+0

包含了''頭。爲了在include之後保持現有的代碼,請添加'using namespace std;'。提示:您可以使用免費的AStyle工具來設置代碼的格式(許多編輯也可以這樣做)。 –

+0

在附註中,換行符和製表符需要反斜槓,而不是前向符號。所以你應該輸入'\ n'和'\ t',而不是'/ n'和'/ t'。 –

+0

此外,在我看來,你的代碼會清除控制檯,甚至可以讀取任何輸出。 (這就是我認爲'系統(「CLS」)')。可能會添加一個等待輸入鍵或類似cin的東西。 –

回答