C++程序产生了许多错误
C++ program produced many errors
好吧,我是一个完全的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.作为一个建议,当你写一个新代码时,试着在你写的每一部分之后编译它,这将帮助你找出是哪段代码导致了错误。
相关文章:
- 在混合代码库中将C转换为C++时出现许多包含错误
- 错误:'int'之前的预期主表达式以及代码中的许多类似错误
- 我的代码运行良好,但现在当尝试将其制作成模板时,我遇到了许多看似无关的错误。这是怎么回事?
- Visual Studio 上的许多错误
- 我的程序显示许多错误,"error: no matching function for call to 'getPercentScore'"
- Qt <winInet.h> 在构建时输出许多 C4430 和 C2146 错误
- 使用许多嵌套的#include语句对编译器错误进行故障排除
- 在使用VS2010构建ACE和TAO(CORBA)时发生了许多错误
- C Wininet FTP列出了许多文件错误
- 编译过程中有许多错误opencv_contrib
- MPI与许多处理器通信时的错误
- 弦装饰班导致许多构建错误
- PMS5003带有ESP8266-许多校验和错误
- 本机 cpp 中存在许多未解决的错误
- OpenGL红色书籍示例1-1错误lnk2019;Glut和Glew的问题以及许多默认的Libs
- 定义字符串时C++许多错误
- C++程序产生了许多错误
- Qt中许多Windows函数的未解决的链接器错误
- C++许多错误.如何解决
- 对许多函数调用进行错误检查