A lot of fraction program in c++

Hi everyone!
My program is working well but the problem when program is complied the output gives -5/-9 instead of -5/9 if I choose 3 for Multiply fraction and the problem with other operation. How I can adjust the negative sign for denominator
if I enter 2, -3 , 5, 6 in order with multiply operation It should be the answer -5/9. please someone help me Thanks for advance.

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//==================================================================
#include <cstdlib> 
#include<iostream>

using namespace std;
//create their own data types. Perhaps the simplest method for doing so is via an 
//enumerated type. An enumerated type is a data type
//where every possible value is defined as a symbolic constant (called an enumerator).
enum Menu_Arthmetic_Choice {
	None          = 0,
	Menu_Add      = 1,
	Menu_Subtract  =2,
	Menu_Multiply = 3,
	Menu_Divide   = 4,
	Menu_Quit     = 5,
	End           = 6,

};

//Passing structure to functions
struct Fractions                     //Holds data for Fractions input
{ 
	int resultNum;
	int resultDen;
	int num1;        
	int num2;
	int den1;
	int den2;
};
//Function Prototypes
int menu();
void addFractions(Fractions, int &resultNum, int &resultDen);
void subtractFractions(Fractions, int &resultNum, int &resultDen);
void multiplyFractions(Fractions, int &resultNum, int &resultDen);
void divideFractions(Fractions, int &resultNum, int &resultDen);
void showResults(Fractions, int resultNum, int resultDen, int operation);
void readFractionValues(Fractions &);
int SimplifyFraction(int &resultNum, int &resultDen);
int Simplify(int &resultNum, int &resultDen);
//int sam(int &resultNum, int &resultDen);

int main()
{
	Fractions input;
	int menuArthmeticChoice = None;
	int resultNum = 0;
	int resultDen = 0;
	
	do
	{
		menuArthmeticChoice = menu();

		// I used if instead of switch statement because it is more efficiency than switch statement
		if (menuArthmeticChoice == Menu_Quit)
			break;
		//Call The functions

		readFractionValues(input);
     //If choice is "addfractions" then
		if (menuArthmeticChoice == Menu_Add)
		{
			addFractions(input, resultNum, resultDen);
		}

		//If choice "subtractfractions" so 
		if (menuArthmeticChoice == Menu_Subtract)
		{
			subtractFractions(input, resultNum, resultDen);
		}

		//If choice "multiplyfraction" so
		if (menuArthmeticChoice == Menu_Multiply)
		{
			multiplyFractions(input, resultNum, resultDen);

		}
		//If choice "divivefraction" so
		if (menuArthmeticChoice == Menu_Divide)
		{
			divideFractions(input, resultNum, resultDen);
		}
		SimplifyFraction(resultNum,resultDen);
		 //Simplify(resultNum, resultDen);
//		 sam(resultNum, resultDen);
		//Show results
		showResults(input, resultNum, resultDen, menuArthmeticChoice);

	} while (menuArthmeticChoice != Menu_Quit);
	return 0;
	
}
/*******************************
*A menu for choice of arithmetic*
*operations, in which the user  *
*can make repeated choices      *
*********************************/
int menu()
{
	int menuChoiceOfArithmetic = 0;
	do
	{
		// 1-Add Fractions
		cout << "1- Add Fraction" << endl;
		//2-Subtract Fractions
		cout << "2- Subtract Fraction" << endl;
		//3-Multiply Fractions
		cout << "3- Multiply Fraction" << endl;
		//4-Divide Fractions 
		cout << "4- Divide Fraction" << endl;
		//5-Quit
		cout << "5-Quit" << endl;
		//make a choice
		cout << "Ensert a number of choice" << endl;
		cin >> menuChoiceOfArithmetic;

		if (menuChoiceOfArithmetic <= None || menuChoiceOfArithmetic >= End)
			cout << "Invalid menu choice. Please try again." << endl;
	} 
	//End Menu_Arthmetic_Choice loop
	while (menuChoiceOfArithmetic <= None || menuChoiceOfArithmetic >= End);

	//Return valid Menu_Arthmetic_Choice 
	return menuChoiceOfArithmetic;
}
/*****************************************************************
*readFractionValues function: gets fraction values directly       *
*from user.                                                       *
******************************************************************/

void readFractionValues(Fractions &input)
{
cout << "Enter denomitor for first fraction: ";
cin >> input.den1;

cout << "Enter numerator for first fraction: ";
cin >>input. num1;

cout << "Enter denomitor for second fraction: ";
cin >> input.den2;

cout << "Enter numerator for second fraction: ";
cin >> input.num2;
cout << endl;

}


/*****************************************************************
*addFractions function: adds two fractions using input provided and*
*display the results.                                             *
******************************************************************/
void addFractions(Fractions input, int &resultNum, int &resultDen)
{
	
	resultNum = (input.den1 * input.num2) + (input.den2 *input. num1);
	resultDen =input. num1 * input.num2;
	
	

}

/*****************************************************************
*subtrctFractions function: subtractes two fractions using input  *
*provided and display the results.                                *
******************************************************************/
void subtractFractions(Fractions input, int &resultNum, int &resultDen)
{
	resultNum = (input.den1 * input.num2) - (input.den2 *input.num1);
	resultDen = input.num1 * input.num2;
}

/*****************************************************************
*multiplyFractions function: multiplies two fractions using input *
*provided and display the results.                                *
******************************************************************/

void multiplyFractions(Fractions input, int &resultNum, int &resultDen)
{
	resultNum = input.den1 * input.den2;
	resultDen = input.num1 * input.num2;
}

/*****************************************************************
*divideFractions function: divides two fractions using input      *
*provided and display the results.                                *
******************************************************************/

void divideFractions(Fractions input, int &resultNum, int &resultDen)
{
	resultNum = input.den1 * input.num2;
	resultDen = input.den2 * input.num1;
}




int SimplifyFraction(int &resultNum, int &resultDen)
{
	if (resultDen <0 && resultNum < 0)
	{
		//resultNum = -resultNum;
		//resultDen =  abs(resultDen);
		int sign = resultNum * resultDen < 0 ? -1 : 1;
		resultNum = sign * abs(resultNum);
		resultDen = abs(resultDen);
	}
	if ((resultNum < 0 && resultDen > 0) || (resultNum > 0 && resultDen < 0))
		//return resultDen;
	{
		if (resultNum > 0)
		{
			resultNum *= -1;
		}
		resultDen = resultDen;
	}

	

	for (int i = resultNum*resultDen; i > 1; i--)
	{
		if ((resultNum % i == 0) && (resultDen % i == 0))
		{
			resultNum /= i;
			resultDen /= i;

		}
	}
	
	
	return (resultNum, resultDen);
	
}


/*****************************************************************
*showResults function:Displays the fractions and results          *
*in equation form.                                                *
******************************************************************/

void showResults(Fractions input, int resultNum, int resultDen, int operation)
{
	//If operation ADD
	if (operation == Menu_Add)
	{
		cout << input.den1 << '/' << input.num1 << '+' << input.den2 << '/' << input.num2 << '=' << resultNum << '/' << resultDen << endl;
	}

	//If operation Subtract
	if (operation == Menu_Subtract)
	{
		cout << input.den1 << '/' << input.num1 << '-' << input.den2 << '/' << input.num2 << '=' << resultNum << '/' << resultDen << endl;
	}
	
	//If operation Multiply
	if (operation == Menu_Multiply)
	{
		cout <<input.den1 << '/' << input.num1 << '*' << input.den2 << '/' << input.num2 << '=' << resultNum << '/' << resultDen << endl;
	}
	//If operation Divide
	if (operation == Menu_Divide)
	{
		cout << input.den1 << '/' << input.num1 << '/' << input.den2 << '/' << input.num2 << '=' << resultNum << '/' << resultDen << endl;
	}
}
I don't understand this part:
1
2
3
4
5
6
7
8
if (resultDen <0 && resultNum < 0)
	{
		//resultNum = -resultNum;
		//resultDen =  abs(resultDen);
		int sign = resultNum * resultDen < 0 ? -1 : 1;   //especiially here
		resultNum = sign * abs(resultNum);
		resultDen = abs(resultDen);
	}

resultDen < 0 ? -1 : 1: this is always true look to the if statement
and why:
1
2
//resultNum = -resultNum;
//resultDen =  abs(resultDen); 
?
why is one - and the other abs
you could write
1
2
3
4
5
if (resultDen <0 && resultNum < 0)
	{
		resultNum = -resultNum;
		resultDen =  -resultDen;
        }



returning to the error
1
2
3
4
5
6
7
8
9
if ((resultNum < 0 && resultDen > 0) || (resultNum > 0 && resultDen < 0))
		//return resultDen;
	{
		if (resultNum > 0)
		{
			resultNum *= -1;
		}
		resultDen = resultDen;   //what is that????
	}

it should be
1
2
3
4
5
else if(resultDen<0)     //we don't care if the numerator only was negative
{
       resultNum=-resultNum;
       resultDen=-resultDen;
}
ooh I did not notice that.
If the denominator in the end result is negative, the fraction should be adjusted so that the denominator is positive and the numerator switches sign value. The calculation result is to be presented in an abbreviated form, and in a neatly output format that if output gives 10/18 it should be 5/9
this is original program after edited
#include <cstdlib>
#include<iostream>
#define TRUE 1
#define FALSE 0

using namespace std;
//create their own data types. Perhaps the simplest method for doing so is via an
//enumerated type. An enumerated type is a data type
//where every possible value is defined as a symbolic constant (called an enumerator).
enum Menu_Arthmetic_Choice {
None = 0,
Menu_Add = 1,
Menu_Subtract =2,
Menu_Multiply = 3,
Menu_Divide = 4,
Menu_Quit = 5,
End = 6,

};

//Passing structure to functions
struct Fractions //Holds data for Fractions input
{
int resultNum;
int resultDen;
int num1;
int num2;
int den1;
int den2;
};
//Function Prototypes
int menu();
void addFractions(Fractions, int &resultNum, int &resultDen);
void subtractFractions(Fractions, int &resultNum, int &resultDen);
void multiplyFractions(Fractions, int &resultNum, int &resultDen);
void divideFractions(Fractions, int &resultNum, int &resultDen);
void showResults(Fractions, int resultNum, int resultDen, int operation);
void readFractionValues(Fractions &);
int SimplifyFraction(int &resultNum, int &resultDen);
int Simplify(int &resultNum, int &resultDen);
//int sam(int &resultNum, int &resultDen);

int main()
{
Fractions input;
int menuArthmeticChoice = None;
int resultNum = 0;
int resultDen = 0;

do
{
menuArthmeticChoice = menu();

// I used if instead of switch statement because it is more efficiency than switch statement
if (menuArthmeticChoice == Menu_Quit)
break;
//Call The functions

readFractionValues(input);
//If choice is "addfractions" then
if (menuArthmeticChoice == Menu_Add)
{
addFractions(input, resultNum, resultDen);
}

//If choice "subtractfractions" so
if (menuArthmeticChoice == Menu_Subtract)
{
subtractFractions(input, resultNum, resultDen);
}

//If choice "multiplyfraction" so
if (menuArthmeticChoice == Menu_Multiply)
{
multiplyFractions(input, resultNum, resultDen);

}
//If choice "divivefraction" so
if (menuArthmeticChoice == Menu_Divide)
{
divideFractions(input, resultNum, resultDen);
}

SimplifyFraction(resultNum, resultDen);


//Simplify(resultNum, resultDen);
// sam(resultNum, resultDen);
//Show results
showResults(input, resultNum, resultDen, menuArthmeticChoice);

} while (menuArthmeticChoice != Menu_Quit);
return 0;

}
/*******************************
*A menu for choice of arithmetic*
*operations, in which the user *
*can make repeated choices *
*********************************/
int menu()
{
int menuChoiceOfArithmetic = 0;
do
{
// 1-Add Fractions
cout << "1- Add Fraction" << endl;
//2-Subtract Fractions
cout << "2- Subtract Fraction" << endl;
//3-Multiply Fractions
cout << "3- Multiply Fraction" << endl;
//4-Divide Fractions
cout << "4- Divide Fraction" << endl;
//5-Quit
cout << "5-Quit" << endl;
//make a choice
cout << "Ensert a number of choice" << endl;
cin >> menuChoiceOfArithmetic;

if (menuChoiceOfArithmetic <= None || menuChoiceOfArithmetic >= End)
cout << "Invalid menu choice. Please try again." << endl;
}
//End Menu_Arthmetic_Choice loop
while (menuChoiceOfArithmetic <= None || menuChoiceOfArithmetic >= End);

//Return valid Menu_Arthmetic_Choice
return menuChoiceOfArithmetic;
}
/*****************************************************************
*readFractionValues function: gets fraction values directly *
*from user. *
******************************************************************/

void readFractionValues(Fractions &input)
{
cout << "Enter denomitor for first fraction: ";
cin >> input.den1;

cout << "Enter numerator for first fraction: ";
cin >>input. num1;

cout << "Enter denomitor for second fraction: ";
cin >> input.den2;

cout << "Enter numerator for second fraction: ";
cin >> input.num2;
cout << endl;

}


/*****************************************************************
*addFractions function: adds two fractions using input provided and*
*display the results. *
******************************************************************/
void addFractions(Fractions input, int &resultNum, int &resultDen)
{

resultNum = (input.den1 * input.num2) + (input.den2 *input. num1);
resultDen =input. num1 * input.num2;



}

/*****************************************************************
*subtrctFractions function: subtractes two fractions using input *
*provided and display the results. *
******************************************************************/
void subtractFractions(Fractions input, int &resultNum, int &resultDen)
{
resultNum = (input.den1 * input.num2) - (input.den2 *input.num1);
resultDen = input.num1 * input.num2;
}

/*****************************************************************
*multiplyFractions function: multiplies two fractions using input *
*provided and display the results. *
******************************************************************/

void multiplyFractions(Fractions input, int &resultNum, int &resultDen)
{
resultNum = input.den1 * input.den2;
resultDen = input.num1 * input.num2;
}

/*****************************************************************
*divideFractions function: divides two fractions using input *
*provided and display the results. *
******************************************************************/

void divideFractions(Fractions input, int &resultNum, int &resultDen)
{
resultNum = input.den1 * input.num2;
resultDen = input.den2 * input.num1;
}



int SimplifyFraction(int &resultNum, int &resultDen)
{
if ((resultNum < 0 && resultDen > 0) || (resultNum > 0 && resultDen < 0))
//return resultDen;
{
if (resultNum > 0)
{

resultNum *= -1;
}
resultDen = resultDen;
}

for (int i = resultNum*resultDen; i > 1; i--)
{
if ((resultNum % i == 0) && (resultDen % i == 0))
{
resultNum /= i;
resultDen /= i;

}
}


return (resultNum, resultDen);

}

/*****************************************************************
*showResults function:Displays the fractions and results *
*in equation form. *
******************************************************************/

void showResults(Fractions input, int resultNum, int resultDen, int operation)
{
//If operation ADD
if (operation == Menu_Add)
{
cout << input.den1 << '/' << input.num1 << '+' << input.den2 << '/' << input.num2 << '=' << resultNum << '/' << resultDen << endl;
}

//If operation Subtract
if (operation == Menu_Subtract)
{
cout << input.den1 << '/' << input.num1 << '-' << input.den2 << '/' << input.num2 << '=' << resultNum << '/' << resultDen << endl;
}

//If operation Multiply
if (operation == Menu_Multiply)
{
cout <<input.den1 << '/' << input.num1 << '*' << input.den2 << '/' << input.num2 << '=' << resultNum << '/' << resultDen << endl;
}
//If operation Divide
if (operation == Menu_Divide)
{
cout << input.den1 << '/' << input.num1 << '/' << input.den2 << '/' << input.num2 << '=' << resultNum << '/' << resultDen << endl;
}
}
i tried to change as you mentioned but it is still the output gives -5/-9 instead of -5/9 :(
int SimplifyFraction(int &resultNum, int &resultDen)
{
if ((resultNum < 0 && resultDen > 0) || (resultNum > 0 && resultDen < 0))
//return resultDen;
{
if (resultNum > 0)
{

resultNum *= -1;
}
else if (resultDen < 0)
{
resultDen = -resultDen;
}
}

for (int i = resultNum*resultDen; i > 1; i--)
{
if ((resultNum % i == 0) && (resultDen % i == 0))
{
resultNum /= i;
resultDen /= i;

}
}


return (resultNum, resultDen);

}
I didn't say like that:
first:please use the code tags
second: you removed that part:
1
2
3
4
5
if (resultDen <0 && resultNum < 0)
	{
		resultNum = -resultNum;
		resultDen =  -resultDen;
        }

third:this wasn't what I said
1
2
3
4
5
6
7
8
9
10
11
12
13
if ((resultNum < 0 && resultDen > 0) || (resultNum > 0 && resultDen < 0))
//return resultDen;
{
if (resultNum > 0)
{

resultNum *= -1;
}
else if (resultDen < 0) //this is always wrong
{
resultDen = -resultDen;
}
}

you said
if ((resultNum < 0 && resultDen > 0) || (resultNum > 0 && resultDen < 0))

if (resultNum > 0) //if(resultNum > 0 && resultDen < 0)
...
else //if(resultNum <= 0)=>if(resultNum < 0 && resultDen > 0)=>if(resultDen>0)

if (resultDen < 0) //resultDen < 0&&resultDen > 0 impossible==>false
Topic archived. No new replies allowed.