2014-08-28 49 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缓冲区。