C++程序产生了许多错误

C++ program produced many errors

本文关键字:错误 许多 产生了 程序 C++      更新时间:2023-10-16

好吧,我是一个完全的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;
}

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

operator是一个关键字-请使用op作为变量名。

您希望使用>>运算符和cin进行输入,而不是使用<<运算符。

很可能是这一行:

string operator;

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

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

A。错误是什么
B.作为一个建议,当你写一个新代码时,试着在你写的每一部分之后编译它,这将帮助你找出是哪段代码导致了错误。