减去std::字符串时出现编译器错误
Compiler error when subtracting std::strings
我是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[Error]与"first_argument-second_argument'中的"operator-"不匹配
我不明白。加号有效,除减法外,其他都有效。所以我搞砸了别的
cout << first_argument << " - " << second_argument << " = " << first_argument - second_argument << endl;
但减法部分效果很好。我不明白。请帮忙
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';
加法部分之所以有效,是因为字符串+字符串会导致字符串字符串。它将这两个字符串相加并返回一个新字符串。
但是减去两个字符串并不意味着什么。
我相信你实际上想做的是把字符串转换成数字,然后减去数字。
要做到这一点,您需要使用以下内容:
double val_1, val_2;
cin >> val_1;
cin >> val_2;
cout << "result is " << (val_1 - val_2) << endl;
我把减法放在括号里,因为我相信<<
,也就是"移位"运算符与乘法处于同一级别,这意味着如果没有它们,它将尝试计算("结果是"<<val_1)-(val_2<<endl)。
由于我不确定运算符优先级,我检查了http://en.cppreference.com/w/cpp/language/operator_precedence并且发现<lt;比减法低,所以我的括号没有必要。
#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;
}
}
使用if-else语句,否则您的代码将没有机会检查用户是否正在尝试减法。最好把其他的留在最后。还将字符串更改为int,因为字符串不是数字,所以不能进行减法。最后,我使用了cin.clear()&cin.ignore()以刷新cin缓冲区。
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 编译器错误:destuctor 的更宽松的抛出说明符
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- C++ G++ 编译器 - 错误:隐式声明的定义
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 模板模板参数和模板别名:编译器错误?
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- C1001内部编译器错误是由于矢量初始化(如数组)引起的
- 编译器错误(英特尔并行工作室 2019 与 Visual Studio 社区 2019)
- 如何在C++中克服此 C4430 编译器错误?
- 尝试使用 WinBGI 显示文本时出现编译器错误
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- C++自定义分配器大小参数作为模板参数会引发编译器错误