Hey guys i need help with this!

Hi guys its kind of long but i've done most of it, i just need help i have functions and i need to pass the returns to other functions for the problem. Just as the directions explain.. and wanna help me. Please and Thank you!
****************************************************************************************************************************************************************
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 the fractions entered to a perform function which performs the operation (i.e: add, subtract, multiply or divide).

Then, the perform function passes the resulting fraction (numerator and denominator) to reduce function to reduce the fraction. 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.

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.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]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*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.
*/


#include <iostream>
#include <ctime>
using namespace std;

int menu_f();
int perform_f(int,int,int,int,int);
int reduce_f(int,int);
int print_f();
int main()
{
	int num1,den1,num2,den2,choice;
	int perform;
	char slash;
	
	
	choice = menu_f();
	if(choice==5)
	{
		system("pause");
		exit(0);
	}	
	else
	{
		cout<<"Enter two fractions (e.g.: 1/2 3/4): ";
		cin>>num1>>slash>>den1>>num2>>slash>>den2;
	}
	perform = perform_f(num1,den1,num2,den2,choice);

system("pause");
}
//*********************************************************\\

int menu_f()
{

	int choice;

	cout<<"1.Add"
		<<"\n2.Subtract"
		<<"\n3.Multiply"
		<<"\n4.Divide"
		<<"\n5.Quit";


	cout<<"\nEnter Selection: ";
	cin>>choice;
	if(choice<=0 || choice>5)
	{
		cout<<"Invalid entry selected! Please re-enter selection:";
		cin>>choice;
	}
	
	if(choice>0 && choice<=5)
			return choice;
}

//******************************************************************//

int perform_f(int num1, int den1, int num2, int den2,int choice)
{
		int snum;
		int sden;

		if(choice==1)
		{
			snum=(num1*den2)+(num2*den1);
			sden= den1*den2;
			return snum,sden;
		}
		if(choice==2)
		{	
			snum=(num1*den2)-(num2*den1);
			sden=den1*den2;
			return snum,sden;
		}
		if(choice==3)
		{
			snum=num1*num2;
			sden=den1*den2;
			return snum,sden;
		}
		if(choice==4)
		{
			snum=(num1*den2);
			sden=(num2*den1);
			return snum,sden;
		}
}
//********************************************************************//

int reduce_f(int snum, int sden)
{
	for(int i=1;i<=sden;i++)
		if(snum%i==0 && sden%i==0)
			{
				snum=snum/i;
				sden=sden/i;
			}
		return snum,sden;
}
//*******************************************************************//
int print_f(int snum, int sden)
{
	char slash='/';
	cout<<endl<<snum<<slash<<sden;
}
Hello


return snum,sden;

You cannot return a pair of int like that, you should either create a struct (or a class) fraction that contains num and den or pass them by reference to the functions that modify them, i.e. you reduce function could be :
1
2
3
4
5
6
7
8
9
void reduce_f(int &snum, int &sden)
{
	for(int i=1;i<=sden;i++)
		if(snum%i==0 && sden%i==0)
			{
				snum=snum/i;
				sden=sden/i;
			}
}


You could also replace the if on line 59 by a while, so that the user can enter an invalid choice more than once.
Hi johnbob, i see what you are saying but if i need to send the snum and sden from perform_f to reduce_f to print_f would that mean it doesn't return anything for all of those if im just passing to functions? I dont get how to do this because using void i need to pass to ints over to reduce_f from perform_f and pass reduce_f to print_f.. idk if u see my point.
If you choose to pass the parameters by reference (&), the functions modifies them and you don't need to return anything, but you will have to add two [out] integer parameters (for the results num and den) to your process function

OR you could create a type holding a fraction, that will be you return type :

1
2
3
4
5
6
7
8
9
10
typedef struct{
    int num;
    int den;
} fraction;

//and then you function could be 

fraction perform_f(fraction f1, fraction f2, int choice)
{...}



btw, your print function should be void, as it doesn't return anything

Last edited on
Topic archived. No new replies allowed.