Adding Fractions! How to align correctly??

i need an output like this...


This program adds fractions. ‘Y’ continues, any other key exits program
===================================================
Enter numerator 1 ==> 1
Enter denominator 1 ==> 3

Enter numerator 2 ==> 1
Enter denominator 2 ==> 6

 1     1     1
--- + --- = ---
 3     6     2
-----------------------------------------------------
Continue? Y or N! ==> n



how can i get the get the right alignment?

Here is 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
97
98
99

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <ctype.h>
#include <locale>

using namespace std;

int EquationNum (int Num1, int Den1, int Num2, int Den2);
int EquationDen (int Num1, int Den1, int Num2, int Den2);
int GreatestCommonDenominator (int Numerator, int Denominator);
void Output (int Num1, int Den1, int Num2, int Den2, int Numerator, int Denominator);
int main()
{
	int Numerator, Denominator, GCD, Num1, Num2, Den1, Den2;
    char Restart;
    do
	{
	cout << "This program adds fractions. 'Y' continues, any other key exits program\n";
	cout << "=======================================================================\n";

	cout << "Enter " << setw(18) << "numerator 1 ==> ";
        cin >> Num1;
		if (isdigit(Num1))
		{
			cout << "you need to enter a number here.";
			cin.clear();
		}
	cout << "Enter denominator 1 ==> ";
        cin >> Den1;
		if (isdigit(Den1))
		{
			cout << "you need to enter a number here.";
			cin.clear();
		}
    cout << endl;

	cout << "Enter " << setw(18) << "numerator 2 ==> ";
        cin >> Num2;
		if (isdigit(Num2))
		{
			cout << "you need to enter a number here.";
			cin.clear();
		}

	cout << "Enter denominator 2 ==> ";
	   cin >> Den2;
	   if (isdigit(Den2))
		{
			cout << "you need to enter a number here.";
			cin.clear();
		}
	   cout << endl;


    Numerator = EquationNum (Num1, Den1, Num2, Den2);
    Denominator = EquationDen (Num1, Den1, Num2, Den2);
    GCD = GreatestCommonDenominator (Numerator, Denominator);

        Numerator = Numerator / GCD;
        Denominator = Denominator / GCD;
	Output (Num1, Den1, Num2, Den2, Numerator, Denominator);

	cout << "-----------------------------------------------------\n";

        cout << "Continue? Y or N! ==> ";
	        cin >> Restart;
        system("cls");
	}
    while (Restart == 'Y' || Restart == 'y');

	}
int EquationNum ( int A, int B, int C, int D)
{
    int Numerator;
		Numerator = A * D + B * C;
			return Numerator;
}
int EquationDen ( int A, int B, int C, int D)
{
    int Denominator;
		Denominator = B * D;
			return Denominator;
}
int GreatestCommonDenominator (int Numerator, int Denominator)
{
    int  GCD = 0;
		for(int i = 1; i <= Numerator && i <= Denominator ; i++)
			if(Numerator % i == 0 && Denominator % i == 0 )
				GCD=i;
			return GCD;
}
void Output (int A, int B, int C, int D, int Numerator, int Denominator)
{
	cout << setw(2) << A << setw(3) << " " << setw(3) << C << setw(3) << " " << setw(3) << Numerator << endl;
	cout << setw(3) << "---" << " + " << setw(3) << "---" << " = " << setw(3) << "---" << endl;
	cout << setw(2) << B << setw(3) << " " << setw(3) << D << setw(3) << " " << setw(3) << Denominator << endl;
}


Last edited on
A good start would be to actually call your misnamed GDC function ;)
Okay i changed it but how can i return the Numerator from function "EquationNum" and Denominatorfrom "EquationDen" to use them with function "GCD"? then make them return to function "Output"?
On lines 25 and 26, you call the functions but you don't take their return values. Try something like this:
25
26
int en = EquationNum (Num1, Den1, Num2, Den2);
int ed = EquationDen (Num1, Den1, Num2, Den2);
Also, why does your Output function have a return type of int? It doesn't return anything, the return type should be void.
Last edited on
Am i getting my assigned variables mixed up? and am i suppose to call the functions

1
2
EquationNum (Num1, Den1, Num2, Den2);
EquationDen (Num1, Den1, Num2, Den2);


and then have..

1
2
int A = EquationNum (Num1, Den1, Num2, Den2);
int B = EquationDen (Num1, Den1, Num2, Den2);


so that they call and then assign?
also i have changed the functions to return A, B. i want A to be returned as the Numerator Added before GCD and B added for the Denominator Added before GCD function.

1
2
3
4
5
6
7
8
9
10
11
12
int EquationNum ( int A, int B, int C, int D)
	{
		int Numerator;
	Numerator = A * D + B * C;
	return A;
	}
int EquationDen ( int A, int B, int C, int D)
	{
		int Denominator;
	Denominator = B * D;
	return B;
	}


Here is my code altogether.


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

using namespace std;

int EquationNum (int Num1, int Den1, int Num2, int Den2);
int EquationDen (int Num1, int Den1, int Num2, int Den2);
int GCD (int A, int B);
void Output (int Num1, int Den1, int Num2, int Den2);
void main()
{
	int Num1, Num2, Den1, Den2, A, B;

	cout << "This program adds fractions. ‘Y’ continues, any other key exits program";
	cout << "===================================================";
	cout << "Enter numerator 1 ==> ";
		cin >> Num1;
	cout << "Enter denominator 1 ==> ";
		cin >> Den1;
	cout << "Enter numerator 2 ==> ";
		cin >> Num2;
	cout << "Enter denominator 2 ==> ";
		cin >> Den2;
		int A = EquationNum (Num1, Den1, Num2, Den2);
		int B = EquationDen (Num1, Den1, Num2, Den2);
		GCD (A, B);
		Output (Num1, Den1, Num2, Den2);
}
int EquationNum ( int A, int B, int C, int D)
	{
		int Numerator;
	Numerator = A * D + B * C;
	return A;
	}
int EquationDen ( int A, int B, int C, int D)
	{
		int Denominator;
	Denominator = B * D;
	return B;
	}
int GCD (int X, int Y)
   {
      int T;       
      while (Y != 0)
      {
         T = Y;
		 Y = X % Y;
         X = T;
      }
      return X;
   }

void Output (int A, int B, int C, int D)
	{
	cout << setw(3) << A << setw(3) << " " << setw(3) << C << setw(3) << " " << setw(3) << EquationNum << endl;
	cout << setw(3) << setfill ('-') << " + " << setw(3) << setfill('-') << " = " << setw(3) << setfill ('-') << endl;
	cout << setw(3) << B << setw(3) << " " << setw(3) << D << setw(3) << " " << setw(3) << EquationDen << endl;
	}
Last edited on
Noob Programmer wrote:
am i suppose to call the functions

1
2
EquationNum (Num1, Den1, Num2, Den2);
EquationDen (Num1, Den1, Num2, Den2);


and then have..

1
2
int A = EquationNum (Num1, Den1, Num2, Den2);
int B = EquationDen (Num1, Den1, Num2, Den2);


so that they call and then assign?
No, that would call the functions twice.
Noob Programmer wrote:
1
2
3
4
5
6
7
8
9
10
11
12
int EquationNum ( int A, int B, int C, int D)
	{
		int Numerator;
	Numerator = A * D + B * C;
	return A;
	}
int EquationDen ( int A, int B, int C, int D)
	{
		int Denominator;
	Denominator = B * D;
	return B;
	}
No, you should return Numerator and Denominator, respectfully. There is no reason to return A, B, C, or D because you already had them in order to give them to the function.

I think you have a lot of misconceptions about how functions work, especially with return values - you should read up on them more.
Ami able to make the line in the middle of the fraction adjust to my numbers?

for a single digit i want it in the middle...


 1
---
 4


but when i input a digit like 4765 i want it to adjust like..

 4765
------
  45
Last edited on
You could convert the numbers to strings, find their lengths, and make a string of dashes based on the longest string's length plus two. You will also need to do some centering work for the smaller string.
Can you give me an example?
Last edited on
Here is what i have.


1
2
3
4
5
6
void Output (int A, int B, int C, int D, int Numerator, int Denominator)
{
	cout << setw(2) << A << setw(3) << " " << setw(3) << C << setw(3) << " " << setw(3) << Numerator << endl;
	cout << setw(3) << "---" << " + " << setw(3) << "---" << " = " << setw(3) << "---" << endl;
	cout << setw(2) << B << setw(3) << " " << setw(3) << D << setw(3) << " " << setw(3) << Denominator << endl;
}


Topic archived. No new replies allowed.