# Thread: Fraction Program...

1. ## Fraction Program...

Hi all. I have been working on a program to add fractions. I want to eventually +, -, *, and / fractions. When I run it all I can do is enter two fractions and nothing else happens. If you have the time I would appreciate any and all help.
Here is the problem:

Write a function that reads a problem involving two common fractions (such as 2/4 + 5/6). After reading, call a function to perform the indicated operation (just adding right now). Pass the numerator and denominator of both fractions to the func that performs the operation (should return num and denom of result through its output parameters). Then display the result as a common fraction.

I have these guidelines:

--foundation: a = bq + r

--1. find gcd of num & denom
2. divid num & denom by gcd
3. use the lcm

--1. divide lcm by denom (multiplier)
2. multiply num & denom by multiplier
3. add num storing in num accumulator
4. copy lcm into denom of accumulator

Code:
--In bold is what I have as guidelines and code
--Also, there are specific questions included as comments
Code:
```#include<iostream>
#include<cmath>
using namespace std;
void getFraction(int&, int&);
void readFracProblem(int&, int&, int&, int&, char&);	//For later use with +,-,*,/
void add(int&, int&, int, int, int);	//I added the last "int" for my program
int gcd(int, int);
int lcm(int, int, int);		//I added the last "int" for program
void normalizeFraction(int&, int&);
void displayFraction(int, int);
int main(){
int n1, d1, n2, d2, gcdNumber;

cout << "Enter a fraction (n / d): ";
getFraction(n1, d1);
cout << "Enter a fraction (n / d): ";
getFraction(n2, d2);

gcdNumber = gcd(d1, d2);

add(n1, d1, n2, d2, gcdNumber);
displayFraction(n1, d1);

return 0;
}

void getFraction(int& n, int& d){
char slash;			//Should it be "char = slash;" ?
cin >> n >> slash >> d;
}

int lcm(int accD, int d, int gcd){
int lcm;
lcm = (accD * d) / gcd;
return lcm;
}

void add(int& accN, int& accD, int n, int d, int gcd){
int lcmNumber = lcm(accD, d, gcd);	//*I'm not sure where these should go
int multiplier = lcmNumber / accD;	//*
accN = accN * multiplier;		//*
multiplier = lcmNumber / d;		//*
n = n * multiplier;			//*
accN = accN + n;			//*
accD = lcmNumber;			//*I'm not sure where these should go
}

int gcd(int a, int b){
int remainder, gcdNumber;
do{
remainder = a % b;
gcdNumber = a / b;
}while(remainder != 0);

return gcdNumber;
}

void normalizeFraction(int& n, int& d){
if(d < 0){
d = -d;
n = -n;
}
int absN = abs(n);
int gcdNumber = gcd(absN, d);
n = n / gcdNumber;
d = d / gcdNumber;
}

void displayFraction(int n, int d){
normalizeFraction(n, d);		//Does this belong here?
cout << n << " / " << d << endl;
}```
I'm still a beginner so.... Thank you for any help

2. Your initial problem is most likely to do with how you get the fractals. You are currently asking the user to input a number, a character and then a number again. However I am not sure this works as you think it does, for this to work the user must either press enter after each number/character or make a space. This because of how the input is handled. At least this is my early suspicioun after looking at your code.

Edit after looking more closely your gcd function is off. if a % b != 0 on entry of the function you are stuck in an infinite loop. Check out wikipedia for an implementation of gcd (found a recursive one that shouldnt be too difficult).

3. When they enter a fraction there is a function (getFraction) that reads in the character but only returns the numerator and denominator. I deleted a cout statement right after the user enters the fractions that I put in to display the two fractions to make sure that worked. They were both displayed right.
Code:
```cout << "Enter a fraction (n / d): ";
getFraction(n2, d2);

cout << n1 << " / " << d1 << endl;
cout << n2 << " / " << d2 << endl;```

4. Ah ok, then that wasnt a problem at least but check my previous post, edited in some more info.

5. Alright thanks, I'll check on that.

6. Even if I change that, am I calling too many functions inside of each other? It feels like I'm messing up with that because I end up with no final n1 and d1 for displayFraction(int n, int d). I have been working on this for about a week and think I've kinda made a mess editing. I just don't really know the order things should go I think. I'm in no rush but cannot wait to finish this. Thanks for the help so far.

7. Your gcd function looks to be wrong. It should not contain a division, only a mod.
Look up the algorithm on wikipedia.

8. Okay thanks. So would the gcd = a%b or gcd = a/b? I don't really understand the formula to find the gcd or lcm. I don't really understand the difference...
Thanks so far though!

9. You can just write one function for gcd, then use the gcd to find the lcm. becuase
lcm(a,b) = a*b/gcd(a,b); something like this would give you the lcm;
Code:
```int lcm(int a, int b)
{
return a*b/gcd(a,b);
}```
In your gcd, you should check if b==0. If it is, return a.

10. oh okay. thanks. I get that

11. Does my add() function look right? Thats all im confused on now. I just dont see how I have no final answer for my displayFraction() function...

12. your add function and display function are ok. The only problem in that code is your gcd function.

13. Okay I changed the gcd() function to:
Code:
```int gcd(int a, int b){
if (b = 0)
return a;
else
return gcd(b, a%b);
}```
but still nothing happens after the user enters the two fractions. Nothing else is displayed and I don't understand why...

14. Originally Posted by hottiefee
Okay I changed the gcd() function to:
Code:
```int gcd(int a, int b){
if (b == 0) // use "==" instead of "="
return a;
else
return gcd(b, a%b);
}```
but still nothing happens after the user enters the two fractions. Nothing else is displayed and I don't understand why...
Use logical "==" instead of assignment.

15. AWESOME! Thanks. I was working on this for a long time. It works completely now. Thank you so much!! Just little details had to be worked out.
Have a great day!

Popular pages Recent additions