ok i have the following c++ and need help to develope a function of the following description :
void addRational(int & ansNum, int& ansDenom)
This function will get 2 rational numbers from the user by calling getRational(), addsthose values and sets the result in ansNum (answer numerator) and ansDenom. It finally
calls displayRational to display the answer.
now i have created the getRational and displayFunction () , i just need to create the function body of addRational(). In my case it is the case 5: on the switch function.
so here is my c++, of what i have so far:
#include<iostre am> //Needed for cout and cin
using namespace std;
void menu(); //Menu-function prototype
void getRational(int &,int&); //Function1 protype
void displayRational (int&,int&);
void addRational(int &,int&);
void decimalEquivale nt(int&, int&);
int main(){
//Declare variables
int choice1,x,y;
static int numer;
static int denom;
do{ // Reiterates switch command until exited
menu();//Call function menu for menu
//Prompt the user for menu choice
cout<<endl<< "Choose one of the option listed above"<<endl;
cin>>choice1;
switch(choice1) {
case 0: cout<<endl<<end l<<"The program will now exit"<<endl<<en dl;
exit(1);
break;
case 1: getRational(num er,denom);//Prompts the user to input numerator and denominator
break;
case 2:
{ if(numer<=0 || denom<=0)//Prompt user to input rational
getRational(num er,denom);
displayRational (numer,denom);//Displays the Rational
break;
}
case 3: addRational(num er,denom);
break;
}
}while(choice1! =0);
return 0;
}
void menu()//Menu Function--displays main menu
{
cout<<endl<<end l<<"1) GetRational "<<endl<<"2 ) Display Rational "<<endl<<"3 ) Add Rational "<<endl;
cout<<"4) Subtract Rational "<<endl<<"5 ) Decimal Equivalent "<<endl<<"0 ) Exit "<<endl<<endl<< endl;
return;
}
void getRational(int & numer, int& denom)//This function prompts user for inputs of numerator and denominator.
{
cout<<endl<<" Please enter the numerator and then the denominator: "<<endl;
cin>>numer>>den om;
return;
}
void displayRational (int& numer, int& denom)
{
cout<<"Your number in rational form is : ";
cout<<numer<<"/"<<denom<<e ndl;
return;
}
void addRational(int & ansNum, int& ansDenom){
return;
}
void decimalEquivale nt(int& numer, int& denom)
{
cout<<"The decimial equivalent of your rational is :";
cout<<(double)n umer/denom<<endl;
return;
}
void addRational(int & ansNum, int& ansDenom)
This function will get 2 rational numbers from the user by calling getRational(), addsthose values and sets the result in ansNum (answer numerator) and ansDenom. It finally
calls displayRational to display the answer.
now i have created the getRational and displayFunction () , i just need to create the function body of addRational(). In my case it is the case 5: on the switch function.
so here is my c++, of what i have so far:
#include<iostre am> //Needed for cout and cin
using namespace std;
void menu(); //Menu-function prototype
void getRational(int &,int&); //Function1 protype
void displayRational (int&,int&);
void addRational(int &,int&);
void decimalEquivale nt(int&, int&);
int main(){
//Declare variables
int choice1,x,y;
static int numer;
static int denom;
do{ // Reiterates switch command until exited
menu();//Call function menu for menu
//Prompt the user for menu choice
cout<<endl<< "Choose one of the option listed above"<<endl;
cin>>choice1;
switch(choice1) {
case 0: cout<<endl<<end l<<"The program will now exit"<<endl<<en dl;
exit(1);
break;
case 1: getRational(num er,denom);//Prompts the user to input numerator and denominator
break;
case 2:
{ if(numer<=0 || denom<=0)//Prompt user to input rational
getRational(num er,denom);
displayRational (numer,denom);//Displays the Rational
break;
}
case 3: addRational(num er,denom);
break;
}
}while(choice1! =0);
return 0;
}
void menu()//Menu Function--displays main menu
{
cout<<endl<<end l<<"1) GetRational "<<endl<<"2 ) Display Rational "<<endl<<"3 ) Add Rational "<<endl;
cout<<"4) Subtract Rational "<<endl<<"5 ) Decimal Equivalent "<<endl<<"0 ) Exit "<<endl<<endl<< endl;
return;
}
void getRational(int & numer, int& denom)//This function prompts user for inputs of numerator and denominator.
{
cout<<endl<<" Please enter the numerator and then the denominator: "<<endl;
cin>>numer>>den om;
return;
}
void displayRational (int& numer, int& denom)
{
cout<<"Your number in rational form is : ";
cout<<numer<<"/"<<denom<<e ndl;
return;
}
void addRational(int & ansNum, int& ansDenom){
return;
}
void decimalEquivale nt(int& numer, int& denom)
{
cout<<"The decimial equivalent of your rational is :";
cout<<(double)n umer/denom<<endl;
return;
}
Comment