Ok, I’m a complete C++ noob (I only started learning yesterday) and I am trying to write a simple calculator program. I wrote it in notepad, but when i tried to compile it, the cmd produced so many errors it was funny. Can anybody tell me what i’m doing wrong?
Here is my code:
#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;
}
operatoris a keyword – useopfor your variable name instead.You want to input using the
>>operator withcin, not the<<operator.