Almost Done..

For some reason, the program will only run the first if/else for the sum, but not for the difference. Idk why, a little assistance please



#include <iostream>
using namespace std;

double getSum(double num1, double num2);
double getDif(double num1, double num2);
double getPro(double num1, double num2);
double getQuo(double num1, double num2);

void displayMenu();
void performAction();

int main()
{
double a,b;
int code;


cout<<"Operation Main Menu"<<endl;
cout<<"-----------------------"<<endl;
cout<<"1.Addition "<<endl;
cout<<"2.Subtract "<<endl;
cout<<"3.Multiplication "<<endl;
cout<<"4.Division "<<endl;
cout<<"5.Exit "<<endl;
cout<<"========================"<<endl;

cout<<"Enter a code to start program (1-5) "<<endl;

cin>>code;

if(code==1)
{
cout<<"Enter 2 values "<<endl;
cin>>a;
cin>>b;
cout<<" The sum of "<<a<<" and "<< b <<" equals: "<<getSum(a,b)<<endl;

}
else(code=!1);


if(code==2)
{
cout<<"Enter 2 values "<<endl;
cin>>a;
cin>>b;
cout<<" The difference of "<<a<<" and "<< b <<" equals: "<<getDif(a,b)<<endl;
}
else(code=!2);




return 0;


}



double getSum(double num1, double num2)
{
double sum;
sum=num1+num2;
return sum;
}

double getDif(double num1, double num2)
{
double dif;
dif=num1-num2;
return dif;
}

What is this?
else(code=!1);
its supposed to say, "if the code is 1, then do whats inside the brackets, but else its NOT 1."
What it really says is "if the code is 1, then do whats inside the brackets, but else assign code to !1 (which is 0)".

Just remove the else part it doesn't need to be there.
Last edited on
Ok, i did that and it worked, my next question is there any way i can get my operation menu to CONTINUOSLY loop so that after the user completes an operation, the menu will continue to show?



#include <iostream>
using namespace std;

double getSum(double num1, double num2);
double getDif(double num1, double num2);
double getPro(double num1, double num2);
double getQuo(double num1, double num2);

void displayMenu();
void performAction();

int main()
{
double a,b;
int code;


cout<<"Operation Main Menu"<<endl;
cout<<"-----------------------"<<endl;
cout<<"1.Addition "<<endl;
cout<<"2.Subtract "<<endl;
cout<<"3.Multiplication "<<endl;
cout<<"4.Division "<<endl;
cout<<"5.Exit "<<endl;
cout<<"========================"<<endl;

cout<<"Enter a code to start program (1-5) "<<endl;

cin>>code;

if(code==1)
{
cout<<"Enter 2 values "<<endl;
cin>>a;
cin>>b;
cout<<" The sum of "<<a<<" and "<< b <<" equals: "<<getSum(a,b)<<endl;

}
else(code==2);


if(code==2)
{
cout<<"Enter 2 values "<<endl;
cin>>a;
cin>>b;
cout<<" The difference of "<<a<<" and "<< b <<" equals: "<<getDif(a,b)<<endl;
}
else(code==3);

if(code==3)
{
cout<<"Enter 2 values "<<endl;
cin>>a;
cin>>b;
cout<<" The product of "<<a<<" and "<<b<<" equals: "<<getPro(a,b)<<endl;
}
else(code==4);

if(code==4)
{
cout<<"Enter 2 values "<<endl;
cin>>a;
cin>>b;
cout<<" The quotient of "<<a<<" and "<<b<<" equals: "<<getQuo(a,b)<<endl;
}
else(code==5);

if(code==5)
{
cout<<" Program closing...."<<endl;

}

else
cout<<"Choose a valid code (1-5)"<<endl;



return 0;


}



double getSum(double num1, double num2)
{
double sum;
sum=num1+num2;
return sum;
}

double getDif(double num1, double num2)
{
double dif;
dif=num1-num2;
return dif;
}

double getPro(double num1, double num2)
{
double x;
x=num1*num2;
return x;
}

double getQuo(double num1, double num2)
{
double q;
q=num1/num2;
return q;
}
Yes, you can put the code inside a loop
I tried that, i put it inside a do..while loop. but my loop never stopped, and I idk how or what conditions to predefine to stop that forever loop.
what about ...
1
2
3
4
do
{
	// code
} while (code!=5);
Nope, that created a forever loop, its keeps repeating the main menu now lol.
You can use the switch case statement instead of if else statement as
1
2
3
4
5
6
7
8
9
10
11
switch ( code ) 
{
	case 1 : 
		//Addition
	case 2 : 
		//Substration
	case 3 : 
		//Multiplication
	case 4: 
		// Division
}
Topic archived. No new replies allowed.