我正在尝试制作一个使用do-while
循环的基本计算器,并提示用户回答用户是否希望从头开始重新运行计算器。
我在cin
遇到yes
或no
的字符串文字答案时遇到以下错误:
<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 :(得分:0)
可以找到问题的解决方案@ source:error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
您需要#include <string>