Dec 2, 2011 at 4:19pm UTC
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;
}
Dec 2, 2011 at 4:21pm UTC
What is this?
else (code=!1);
Dec 2, 2011 at 4:23pm UTC
its supposed to say, "if the code is 1, then do whats inside the brackets, but else its NOT 1."
Dec 2, 2011 at 4:27pm UTC
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 Dec 2, 2011 at 4:28pm UTC
Dec 2, 2011 at 4:34pm UTC
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;
}
Dec 2, 2011 at 4:40pm UTC
Yes, you can put the code inside a loop
Dec 2, 2011 at 4:41pm UTC
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.
Dec 2, 2011 at 4:53pm UTC
Nope, that created a forever loop, its keeps repeating the main menu now lol.