/*
Write a program which continueously calls a menu function which prints the following menu and returns to main the operation selected:
1.Add
2.Subtract
3.Multiply
4.Divide
5.Compare
6.Quit
The menu function must keep printing the menu and asking for a selection, as long as the user enters an invalid entry.
Then, once a valid selection is made, it returns the selection to main.
If the selection is not to quit, main reads two fractions such as 1/2 and 3/4, and as long as the denominator is not 0, passes the
selected operation (1-4) as well as the numerator and denominator of both fractions entered to a perform function which performs
the operation (i.e: add, subtract, multiply or divide). If the operation selected by the user is 5 (compare), it passes the numerator
and denominator of both fractions to compare function which returns 0 if they are equal, 1 if the first fraction is greater than
the second, and -1 if the second one is greater than the first. Then, using the returned value, main prints which fraction is greater
or that they are equal.
After performing the selected operation, the perform function passes the resulting fraction (numerator and denominator)
to reduce function to reduce the fraction. For example, if the fraction passed to reduce is 6/8, it reduces it to 3/4. The reduce
function in turn passes the numerator and denominator of the reduced fraction to print function which prints the fraction.
The program continues until the user selects quit from the menu.
So, you must write the following functions:
main - calls the menu function and gets back the selection; and if the selection is not to quit, it then reads two fractions
and passes them to the perform function.
menu - prints the menu and returns the selection to main.
perform - receives the selection and two fractions from main, performs the operation and passes the resulting fraction to reduce function.
reduce - receives the fraction from perform, reduces it and passes the reduced fraction to print for printing.
print - receives the reduced fraction from reduce and prints it.
compare - receives two fractions from main and returns 0, 1 or -1.
The following is a sample run of the program:
Select an operation from the following menu: [This is done by the menu function]
1.Add
2.Subtract
3.Multiply
4.Divide
5.Compare
6.Quit
Enter your selection: 1
Enter two fractions (e.g.: 1/2 3/4) 1/4 1/2 [This is done in main]
1/4 + 1/2 = 3/4
[Addition is done in perform function, reduction in reduce and printing in print function]
Select an operation from the following menu:
1.Add
2.Subtract
3.Multiply
4.Divide
5.Compare
6.Quit
Enter your selection: 5
Enter two fractions (e.g.: 1/2 3/4): 3/7 9/23
3/7 is larger.
Select an operation from the following menu:
1.Add
2.Subtract
3.Multiply
4.Divide
5.Compare
6.Quit
Enter your selection: 6
Press any key to continue.
*/
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
void addition();
void subtraction();
void multiplication();
void division();
void comparison();
void reduce();
void print();
void menu();
int choice,sum;
int n1,d1,n2,d2,n3,d3;
char slash;
int main()
{
do
{
menu();
cin>>choice;
while(choice<1||choice>6)
{
cout<<endl;
cout<<"Please enter 1,2,3,4,5 or 6: ";
cin>>choice;
}
if(choice!=6)
{
cout<<"\n\nEnter the first fraction: ";
cin>>n1>>slash>>d1;
cout<<"\nEnter the second fraction: ";
cin>>n2>>slash>>d2;
while(choice<=4&&choice>=1)
{
while(d1==0||d2==0)
{
cout<<"\n\tYou cannot have a '0' as a denominator!\n";
cout<<"\nEnter the first fraction: ";
cin>>n1>>slash>>d1;
cout<<"\nEnter the second fraction: ";
cin>>n2>>slash>>d2;
}
}
}
switch(choice)
{
case 1:addition();
break;
case 2:subtraction();
break;
case 3:multiplication();
break;
case 4:division();
break;
//case 5:comparison();
//break;
}
}while(choice!=6);
cout<<endl<<endl;
cout<<"Program is ending...\n\n";
system("pause");
return 0;
}
void menu()
{
cout<<"\n\t\t\t\tMath Function(s) Menu\n\n";
cout<<"1. Add\n";
cout<<"2. Subtract\n";
cout<<"3. Multiply\n";
cout<<"4. Divide\n";
cout<<"5. Compare\n";
cout<<"6. Quit\n";
cout<<"\n\tEnter your choice: ";
}
void addition()
{
n3=(n1*d2)+(n2*d1);
d3=d1*d2;
}
void subtraction()
{
n3=(n1*d2)-(n2*d1);
d3=d1*d2;
}
void multiplication()
{
n3=n1*n2;
d3=d1*d2;
}
void division()
{
n3=(n1*d2);
d3=(n2*d1);
}
/*void comparison()
{
float answer;
if(subtraction((sub>0)))
answer=1;
if(subtraction((sub==0)))
answer=0;
if(subtraction((sub<0)))
answer=-1;
*///}
void reduce()
{
for(int i=1;i<=d3;i++)
if(n3%i==0 && d3%i==0)
{
n3=n3/i;
d3=d3/i;
}
}
void print()
{
cout<<n3<<slash<<d3;
}
newCoder1545
- 3 Contributors
- forum2 Replies
- 3 Views
- 7 Years Discussion Span
- comment Latest Post by chiwawa10
flukebob
Go here to start reading about it, but the help that came with your compiler should be sufficient.
http://msdn.microsoft.com/en-us/library/9w92dxk3(v=VS.71).aspx
Your functions all say "void".
That means they return nothing...
What you need to know is how to declare a function that takes parameters and returns something.
Like, a function that adds might be called "plusser", and take two parameters. The RETURNED value goes into a variable like "summ" in the function that called it. So in your case, you want something that looks like
summ = plusser(num1, num2);
in your main, which calls the function and PASSES the main variables num1 and num2 INTO the integer variables that are declared in your prototype:
int plusser(int x, int y);
The function itself also includes the code that makes it "go":
int plusser(int x, int y) //note there is no semicolon here.
{ //num1 is passed into x, num2 into y
return x+y; //their sum is passed to the integer "summ" in the main
}
The very first keyword "int" just before the function name "plusser" tells the compiler what type of variable it should return to.
I don't have a compiler here to check things for sure, but I don't think I've steered you wrong.
For pete's sake, make your example smaller! I'd suggest using my stuff glued into a small program that adds two numbers through a function call. And don't bother with inputting numbers from the console... just declare them like
int num1 = 7;
and then use a cout to show the sum.
chiwawa10 77
Please describe your problem. What's the problem you're facing. Only then, people will know how to help