-5

好的,我是一个完整的 C++ 菜鸟(我昨天才开始学习),我正在尝试编写一个简单的计算器程序。我用记事本写的,但是当我尝试编译它时,cmd产生了很多错误,这很有趣。谁能告诉我我做错了什么?

这是我的代码:

#include <iostream>
#include <string>
using namespace std;




int main()
{
double num1;
double num2;
string operator;
double num3;

cout<<"Enter your first number"<<endl;
cin<<num1;
cout<<"Enter the operator"<<endl;
cin<<operator;
cout<<"Enter the next number"<<endl;
cin<<num2;

if(operator=="/"&&num2==0)
{
cout<<"You are attempting to divide by 0. This is impossible and causes the destruction of the universe. However, the answer is infinity"<<endl;
}


if(operator=="+")
{
num3 = num1+num2;
}
else if(operator=="-")
{
num3 = num1-num2;
}
else if(operator=="*"||operator=="x"||operator=="X")
{
num3 = num1*num2;
}
else
{
num3 = num1/num2;
}
return 0;
}
4

5 回答 5

3

主要错误是这operator是 C++ 中的关键字,您不能将其用作变量名。尝试将其重命名为op或其他内容。编译器经常会感到困惑,只关注前几个错误,修复它们并重新编译。

于 2012-07-29T08:36:40.837 回答
2

operator是一个关键字 -op用于您的变量名。

您想使用>>带有 的运算符而cin不是<<运算符进行输入。

于 2012-07-29T08:38:20.427 回答
1

很可能是这一行:

string operator;

operator是 C++ 关键字。尝试将其更改为其他名称,例如userOp.

于 2012-07-29T08:36:45.820 回答
0

要从流中读取,您需要>>,所以它是 cin>>num1;

于 2012-07-29T08:37:58.180 回答
0

A. 有什么错误?
B. 作为对新手(如您自称)的建议,当您编写新代码时,请尝试在编写每个部分后对其进行编译,这将帮助您找出导致错误的代码段。

于 2012-07-29T08:39:18.007 回答