Problem outputting a fraction instead of a decimal

I am writing a program that takes from the user 2 numbers to represent the numerator and the denominator of a fraction, and it will then check if any of the numbers are negative, if they are the program will end and ask the user if he or she wants to try again. If the numerator is zero, the program will display a zero, if the denominator is a zero, the program will display "Your fraction is undefined." If the result is false for both of those, it checks if the numerator is greater than the denominator, if it is, the program will calculate and display a mixed number. The problem I have is reducing a fraction more than once, especially when the denominator is greater and the fraction can be reduced. If you guys can tell me what I am doing wrong or missing, thanks! The section concerning if the denominator is greater is blank because I am stuck on what to put there. I originally had it to where it would display a decimal, but I need it to be a fraction, so I deleted that part while trying to think of a way to display a fraction.

P.S. I am fairly new to programming, and I have not learned about functions yet, so please keep that in mind.

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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{	//Here is where I initialize my variables.
	int numerator = 0;
	int denominator = 0;
	int divisor = 0;
	int remainder = 0;
	char userChoice = 0;

	cout << "\t\t***REDUCE A FRACTION***\n" << endl;

	do
	{	//Prompt.
		cout << "Please enter the numerator of a fraction: ";
		cin >> numerator;
		cout << "\n\nNow enter the denominator: ";
		cin >> denominator;

		//If either number is negative.
		if (numerator < 0 || denominator < 0)
		{	//Error message.
			cout << "\n\nPlease do not enter any negatives.\n" 
                             << endl;
		}
		else if (numerator >= 0 && denominator >= 0)
		{	//If either number is equal to zero.
			if (numerator == 0 || denominator == 0)
			{
				if (numerator == 0)
					cout << "\n\n0\n" << endl;

				if (denominator == 0)
					cout << "\n\nYour fraction is undefined.\n" << endl;
			}
			else if (numerator > denominator || denominator > numerator)
			{	
				

				//If numerator is greater than denominator.
				if (numerator > denominator)
				{
					divisor = numerator / denominator;
					remainder = numerator % denominator;

					if (remainder != 0)
					{
						cout << "\n\n" << divisor
							<< " " << remainder
							<< "/" << denominator
							<< '\n' << endl;
					}
					else
						cout << "\n\n" << divisor << '\n' << endl;
				}
				else if (denominator > numerator)
				{
	
				}
			}
		}

		cout << "Would you like to enter another fraction? (y/n): ";
		cin >> userChoice;
		cout << '\n' << endl;

	} while (userChoice == 'y' || userChoice == 'Y');

	cout << "###END OF PROGRAM###\n" << endl;

	return 0;
}
Last edited on
closed account (48T7M4Gy)
Maybe what I wrote in this thread might help.

http://www.cplusplus.com/forum/beginner/146987/
Topic archived. No new replies allowed.