【发布时间】:2026-01-16 10:05:01
【问题描述】:
我正在尝试创建一个计算器,但发生了这种情况.. 这是我的代码。请帮我解决它并给出一些解释:
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
string c;
string d;
cout<<"Enter No. 1: ";
cin>>a;
cout<<"Enter Operation: ";
cin>>c;
cout<<"Enter No. 2: ";
cin>>b;
cout<<"So you want me to solve this: ";
cout<<a<<c<<b;
cout<<"Type Yes or No";
cin>>d;
if(d="yes"){
switch(c)
{
case '+':
cout << a+b;
break;
case '-':
cout << a-b;
break;
case '*':
cout << a*b;
break;
case '/':
cout << a/b;
break;
}
}
else{
return 0;
}
}
这是编译时的代码错误,请修复此代码 ima noob:
main.cpp: In function ‘int main()’:
main.cpp:21:9: error: could not convert ‘d.std::basic_string<_CharT, _Traits, _Alloc>::operator=, std::allocator >(((const char*)"yes"))’ from ‘std::basic_string’ to ‘bool’
if(d="yes"){
~^~~~~~
main.cpp:22:17: error: switch quantity not an integer
switch(c)
【问题讨论】:
-
if(d="yes")应该是if(d=="yes")。而且你不能在switch中使用std::string。看来您希望c属于char类型。 -
你的“礼物”完全离题,应该删除。
-
如果有疑问,请拨打tour 并查看How to Ask。
标签: c++ if-statement switch-statement calculator