0

Why is the if Statement does not work?

#include <iostream> using namespace std; int main() { int m; int n=1; int o=2; int p=3; int q=4; cout<<"Type:"<<endl<<endl; cout<<"1 to sum"<<endl<<endl; cout<<"2 to subtract"<<endl<<endl; cout<<"3 to multiplicate"<<endl<<endl; cout<<"4 to divide"<<endl<<endl; cin>>m; if (m==n); { //addiction cout << "Addiction" << endl; int a; cout << "Enter the first number"<<endl; cin >> a; int b; cout << "Enter the second Number"<<endl; cin >> b; int c=a+b; cout <<"Acording to Kevin the result is:"<<c<<endl<<endl; } //addiction if (m==o); { //subtraction cout << "Subtraction"<<endl; int d; cout <<"Enter First number"<<endl; cin >>d; int e; cout <<"Enter second number"<<endl; cin >> e; int f=d-e; cout << "The result is:"<<f<<endl<<endl; } //subtraction if (m==p); { //Multiplication cout << "Multiplication"<<endl; int g; cout <<"Enter First number"<<endl; cin >>g; int h; cout <<"Enter second number"<<endl; cin >> h; int i=g*h; cout << "The result is:"<<i<<endl<<endl; } //Multiplication if (m==q); { //Division cout << "Division"<<endl; int k; cout <<"Enter First number"<<endl; cin >>k; int l; cout <<"Enter second number"<<endl; cin >> l; int j=k/l; cout << "The result is:"<<j<<endl; //Division return 0; } } This is my attempt to make a calculator.Now as you see in the first cout, I programed to the user put a number and, hopefully it will take you to the desired code. Example: If you write 1, It will take you to the addiction part If you write 2, It will take you to the subtraction part. But it is not working out.It always shows me the addiction part independently the number I put.Please Help me Sorting out the Problem.

20th Jul 2017, 10:32 AM
Kevin Paresh
4 Answers
+ 1
Just tried it now and It worked just perfectly!! Thanks Naveed Jeelani Khan for the opinion!!
20th Jul 2017, 12:55 PM
Kevin Paresh
0
you are terminating" if(m==n);if(m==o);if(m==p);if(m==q);"...remove terminators like if (m==n) { ------- }... also i think it is better to use else if ladder in your case
20th Jul 2017, 10:49 AM
Naveed Jeelani Khan
Naveed Jeelani Khan - avatar
0
there is a syntax error in all ifs . It should be if ( condition) {body} i.e. no semicolon
20th Jul 2017, 11:20 AM
Shibopie
Shibopie - avatar
0
ok Thank you
20th Jul 2017, 12:48 PM
Kevin Paresh