【发布时间】:2014-10-22 21:55:25
【问题描述】:
我是 C++ 的初学者,所以我只是在阅读文章和书籍时搞砸了一些东西。但我花了 20 分钟一遍又一遍地重新阅读这篇文章,我不知道它有什么问题。
#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\Jason\Desktop\Lesson - Header Files\LH1.cpp [错误] no match for 'operator-' in 'first_argument - second_argument'
我不明白。加法符号有效,除减法之外的一切都有效。 所以我搞砸了别的东西
cout << first_argument << " - " << second_argument << " = " << first_argument - second_argument << endl;
但是减法部分工作正常。我不明白。请帮忙
【问题讨论】:
-
请想一个更具描述性的标题。这个标题可能适用于数千个问题。
-
std::string没有operator-。 -
+运算符用于std::string进行连接。没有operator-,我不知道你认为它应该做什么。 -
您希望两个字符串相减会产生什么结果?将 string1 替换为 string2 为 ""?
-
看看你的“加法”的输出。您应该注意到它没有执行算术运算,而是两个输入字符串的连接。以类似的方式,两个字符串的减法不会进行算术运算,并且您没有将减法运算符添加到字符串的派生子类中,因此编译器只是让您知道它不知道如何将两个字符串相减。跨度>
标签: c++