FlyingZeth
FlyingZeth

Reputation: 1

Fractions Program - Issues with returning and more

Just to start thank you very much for helping me with this program. I am very new to C++ and I could really use some help. This program is designed to take two fractions (for this example lets use 1/2 and 1/4) and add, subtract, multiply, and divide the two fractions. So far I have only gotten to the addition part but I am already confused. The output for the addition is 0/0 and not 3/4. I'm not sure why this is happening and I need some help. Also I am try to use and get used to using structs so if you discover a solution to the problem without using structs please do not submit your answer.

Please help me correct what ever is wrong!

#include <cstdlib>
#include <iostream>
#include <math.h>

/*
Name: Fraction
Author: 
Date: 13/10/14 17:33
Description: Takes two fractions and outputs them in different ways
*/


using namespace std;

struct frac {
   int A, B, C, D;
};

frac new_frac () ;
frac addition_frac () ;
frac subtraction_frac () ;
frac multiply_frac () ;
frac divide_frac () ;
void printAdd (frac add) ;

int main(int argc, char *argv[])
{   
frac fraction;
frac add;
new_frac () ;
addition_frac();
cout << "Addition = " ;
printAdd (add) ;

system("PAUSE");
return EXIT_SUCCESS;
}

// A function that asks the user for a fraction (Ex: 1/2 and 1/4)

frac new_frac () {
 frac fraction;
 int Aa;
 int Ab;
 int Ba;
 int Bb;
 cout << "Enter first numerator " ;
 cin >> Aa;
 cout << "Enter first denominator " ;
 cin >> Ab;
 cout << "Enter second numerator " ;
 cin >> Ba;
 cout << "Enter second denominator " ;
 cin >> Bb;
 fraction.A = Aa;
 fraction.B = Ab;
 fraction.C = Ba;
 fraction.D = Bb;
 cout << "Fraction 1 = " << fraction.A << "/" << fraction.B ;
 cout << endl;
 cout << "Fraction 2 = " << fraction.C << "/" << fraction.D ;
 cout << endl;

 return fraction;

}

// A function to add the fractions

frac addition_frac () {
 frac add ;
 frac fraction ;
 add.A = (fraction.A * fraction.B) + (fraction.C * fraction.D) ;
 add.B = fraction.A * fraction.D ;

 return add;
}

void printAdd (frac add) {
 frac fraction;
 cout << add.A << "/" << add.B << endl ;
 }

Upvotes: 0

Views: 133

Answers (2)

Daniel L
Daniel L

Reputation: 178

Your new_frac() and addition_frac() functions each have their own local variables. It looks like you were trying to deal with them as if they were global variables.

I would suggest your struct to contain only two numbers (one numerator and one denominator), and then have a function to add them with the signature frac add_frac(frac frac1, frac frac2) and call it from main like so: frac sum = add_frac(first, second), and then call your function for printing a fraction with the result. Your struct should also have a constructor for building a new fraction like so: frac first(1, 2).

Hope this helps.

Upvotes: 1

ravi
ravi

Reputation: 10733

First of all you have just declared type ( struct frac ) globally whereas you are defining variables locally ( inside main ). That means you have to pass these variables to another function by reference if you want that function to change your variables ( like in this case you want your function to fill in values into your variable ).

frac fraction;
frac add;
new_frac (frac& fraction, frac& add) ;                      //assuming frac is typedefed.
addition_frac (frac& fraction, frac& add);

Upvotes: 1

Related Questions