2014-08-28 44 views
0

我是C++的初學者,所以我只是在閱讀文章和書籍時將一些東西搞亂了。但我花了20分鐘時間重複閱讀這些內容,我無法分辨出它有什麼問題。編譯器在減去std :: strings時出錯

#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{ 

    cout << "Hello there, this is your personal simple calculator."; 
    cin.get(); 
    cout << "Type in what you want to do. (Addition, Subtraction, Multiplication, Division)"<< endl; 
    string c; 
    getline (cin, c); 

    if (c == "Addition") 
    { 
     string a_1; 
     string a_2; 
     cout << "You chose addition. Press enter" << endl ; 
     cin.get(); 
     cout << "Type in the first value: "; 
     getline(cin, a_1); 
     cout << "Type in the second value: "; 
     getline (cin, a_2); 
     cout << a_1 << " + " << a_2 << " = " << a_1 + a_2 << endl; 
    } 
    else 
    { 
     cout << "You spelled it wrong."; 
     return 0; 
    } 
    if (c == "Subtraction") 
    { 
     string s_1; 
     string s_2; 
     cout << "You chose subtraction. Press enter" << endl ; 
     cin.get(); 
     cout << "Type in the first value: "; 
     getline (cin, s_1); 
     cout << "Type in the second value: "; 
     getline (cin, s_2); 
     cout << s_1 << " - " << s_2 << " = " << s_1 - s_2 << endl; 
    } 

} 

我得到這個作爲唯一的錯誤

42 83 C:\ Users \用戶傑森\桌面\課 - 頭文件\ LH1.cpp [錯誤]敵不過在 '操作符 - ' 'first_argument - second_argument'

我不明白。加法符號起作用,除了減法運算以外的所有內容。 所以我用別的

cout << first_argument << " - " << second_argument << " = " << first_argument - second_argument << endl; 

亂七八糟的東西,但周圍是減法部分工作正常。我不明白。請幫助

+0

請考慮一個更具描述性的標題。這個頭銜可能適用於數千個問題。 – chris 2014-08-28 22:23:15

+3

'std :: string'沒有'operator-'。 – juanchopanza 2014-08-28 22:27:02

+4

'std :: string'的'+'運算符是串聯的。沒有'operator-',我不知道你應該怎麼做。 – Blastfurnace 2014-08-28 22:27:34

回答

3

string可以處理文字。當您添加兩個字符串時,它們連接在一起("2"+"2"=="22",而不是"4")。字符串沒有operator-

要處理浮點數,請使用double。爲了應對整數,使用int

double d1, d2; 
//some output 
cin >> d1; 
//some output 
cin >> d2; 
cout << d1 << " - " << d2 << " = " << (d1-d2) << '\n'; 
1

的另外一部分工作,因爲字符串+字符串結果stringstring。它追加兩個字符串並返回一個新的字符串。

但是減去兩個字符串並不意味着什麼。

我相信你實際上想要做的是將字符串轉換爲數字,然後減去數字。

要做到這一點,你需要使用類似以下內容:

double val_1, val_2; 
cin >> val_1; 
cin >> val_2; 

cout << "result is " << (val_1 - val_2) << endl; 

我把裏面的括號內的減法,因爲我相信<<又名「轉移」運營商在同一水平上的乘法,這意味着如果沒有他們,它會嘗試評估(「結果是」< < val_1) - (val_2 < < end1)。

由於我不確定運營商的優先順序,我檢查了http://en.cppreference.com/w/cpp/language/operator_precedence,發現< <低於減法,所以我的括號沒有必要。

0
#include <iostream> 
#include <string> 
#include <limits> 

using namespace std; 

int main() 
{ 

    cout << "Hello there, this is your personal simple calculator."; 
    cin.get(); 
    cout << "Type in what you want to do. (Addition, Subtraction, Multiplication, Division)" << endl; 
    string c; 
    getline(cin, c); 

    if (c == "Addition") 
    { 
     int a_1; 
     int a_2; 
     cout << "You chose addition. Press enter" << endl; 
     cin.get(); 
     cout << "Type in the first value: "; 
     cin >> a_1; 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     cout << "Type in the second value: "; 
     cin >> a_2; 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     cout << a_1 << " + " << a_2 << " = " << a_1 + a_2 << endl; 
    } 

    else if (c == "Subtraction") 
    { 
     int s_1; 
     int s_2; 
     cout << "You chose subtraction. Press enter" << endl; 
     cin.get(); 
     cout << "Type in the first value: "; 
     cin >> s_1; 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     cout << "Type in the second value: "; 
     cin >> s_2; 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     cout << s_1 << " - " << s_2 << " = " << s_1 - s_2 << endl; 
    } 
    else 
    { 
     cout << "You spelled it wrong."; 
     return 0; 
    } 
} 

如果else語句使用,否則您的代碼將沒有機會檢查用戶是否嘗試減法。最好把其他東西留在最後。 還將字符串更改爲整數,因爲字符串不能減去,因爲它們不是數字。 最後,我使用cin.clear()來清除cin緩衝區。