if else statement

I'm trying to make a calculator using if else but it's not working fine. If i remove char 1,2,3,4 the code compiles and runs but after giving 2 values and when I give the operation( int c;) it only gives me the addition result no matter what operation I ask for. if I include char 1,2,3,4 then it gives me 4 syntax errors. i don't know what is the problem please help me.
Or if this can work with switch statement then please tell me how it can apply. I'm new to C++.

#include<iostream>
using namespace std;
int main()
{
int a;
int b;
int c;
char 1;
char 2;
char 3;
char 4;




cout<<"Enter The First Value"<<endl;
cin>>a;
cout<<"Enter The Second Value"<<endl;
cin>>b;

cout<<"Enter 1 to Add"<<endl;
cout<<"Enter 2 to Subtract"<<endl;
cout<<"Enter 3 to Multiply"<<endl;
cout<<"Enter 4 to Divide"<<endl;
cin>>c;

if(c=1)
{cout<<"The Sum Is: "<<a+b<<endl;}
else if(c=2)
{cout<<"The Difference Is: "<<a-b<<endl;}
else if(c=3)
{cout<<"The Multiplication Is: "<<a*b<<endl;}
else if(c=4)
{cout<<"The Product Is: "<<a/b<<endl;}

else
{cout<<"Wrong Entry"<<endl;}

return 0;
}
A variable name can't start with a number. char 1 is invalid syntax. You can name them c1, c2, etc

To compare values you use ==, not =.
= is for assignments
Last edited on
1
2
Also, please always use code tags - select yourr code then press the <> button on the right.


You can use a switch to process the menu options. The menu display should be in it's own function.

a / b is division, not product. Always check for division by zero. Division won't work well with integer types - use double instead. This will be better for the other operations as well.

HTH
Last edited on
Topic archived. No new replies allowed.