Code for shortening a fraction + program stops for no reason.

Hello!
I have been working on a program that can add two fractions together. But I have some trouble with my code and how to make a code that can shorten a fraction. I have tried to search on the internet for solutions for the fraction shortening code, but non of them really worked.

I have tried the best I could to explain my code, but I'm still learning to improve it and I would be more then happy for feedback on my code :)

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
#include <iostream>


int main()
{
	/*"menu" for the code*/
	int fraction_choice;
	std::cout << "1 - add two fractions\n" << std::endl;
	std::cout << "2 - takeaway two fractions\n" << std::endl;
	std::cout << "Please enter your choise\n" << std::endl;
		std::cin >> fraction_choice; //input from user
	switch (fraction_choice)
	{

	case 1://adds tow fractions together
		int a1, b1, a2, b2; //decalring variables, 
		// A stands for the numerator and B stands for the denominator. The number is there to show what fraction it is. (vv guide under vv) 
		/*
		 first fraction					 secound fraction 
			 a1 = x						      a2 =  x
				 ---           +		    	   ---
			 b1 = y						      b2 =  y
		*/

		/*input from user*/
		std::cout << "please enter the numerator and the denominator of the first fraction\n";
		std::cin >> a1;
		std::cout << "/\n";
		std::cin >> b1;
		std::cout << "plase enter the numerator and the denominator of the secound fraction\n";
		std::cin >> a2;
		std::cout << "/\n";
		std::cin >> b2;

		int result_1, result_2; //decalring variables for result of the fraction 
		/*
		result 1 is the numerator
		result 2 is the denominator
		*/
		if (b1 == b2) // is the denominator the same? if yes then just taking the sum of the two numerator
		{
			result_1 = a1 + a2; // sum of the two numerator
			result_2 = b1; //asagin result2 to value of denominator

		}
		else // is the denominators not the same?
		{

			int F1, F2; //variables for "intercompany" 

			F1 = a1 * b2; // F1 stores the result of the numerator for the first fraction times the denominator for the secound fraction. 
			F2 = b1 * a2; // F2 stores the result of the numerator for the secound fraction times the denominator for the first fraction.

			result_1 = F1 + F2; //takes the sum of F1 and F2.
			result_2 = b1 * b2; //takes the product of both denominator in each fraction.
	
		}

		std::cout << "The result is: " << result_1 << "/" << result_2 << std::endl; //output for result

		int Shorten; //secound menu for shorening the found fraction
		std::cout << "do you wish to shorten the fraction?\n";
			std::cout << "1 - Yes\n";
			std::cout << "2 - No\n";
			std::cout << "Please enter your choise\n" << std::endl;	
			std::cin >> Shorten;
			
			// Dont want to run the code from now on??
			switch (Shorten)
			
			{
			case 1:
			{
				// no idea how to make a code for shortening a fraction
			}
				break;
			case 2: // stops the program
				{
				break;
				}
			default:
				std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
				std::cout << "Not a Valid Choice. \n";
				std::cout << "Choose again.\n";
				std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
				std::cin >> Shorten;
				break;
			}

	//case 2: *not sartet*

	}	

	return 0;

}


-Johannes
Last edited on
Geez... It's "numerator" and "denominator", FFS. You spelled each word differently each time!

A fraction of the form n/m can be reduced by dividing n and m by their greatest common divisor. The GCD of two numbers is found by by Euclidean algorithm.
(Obviously the fraction is irreducible if the GCD is 1.)

https://en.wikipedia.org/wiki/Euclidean_algorithm
Topic archived. No new replies allowed.