c++ - Fractions Program - Issues with returning and more -
c++ - Fractions Program - Issues with returning and more -
just start give thanks much helping me program. new c++ , utilize help. programme designed take 2 fractions (for illustration lets utilize 1/2 , 1/4) , add, subtract, multiply, , split 2 fractions. far have gotten add-on part confused. output add-on 0/0 , not 3/4. i'm not sure why happening , need help. seek utilize , used using structs if find solution problem without using structs please not submit answer.
please help me right ever wrong!
#include <cstdlib> #include <iostream> #include <math.h> /* name: fraction author: date: 13/10/14 17:33 description: takes 2 fractions , 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"); homecoming exit_success; } // function asks user fraction (ex: 1/2 , 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 sec numerator " ; cin >> ba; cout << "enter sec 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; homecoming fraction; } // function add together fractions frac addition_frac () { frac add together ; frac fraction ; add.a = (fraction.a * fraction.b) + (fraction.c * fraction.d) ; add.b = fraction.a * fraction.d ; homecoming add; } void printadd (frac add) { frac fraction; cout << add.a << "/" << add.b << endl ; }
first of have declared type ( struct frac ) globally whereas defining variables locally ( within main ). means have pass these variables function by reference if want function alter variables ( in case want function fill in values variable ).
frac fraction; frac add; new_frac (frac& fraction, frac& add) ; //assuming frac typedefed. addition_frac (frac& fraction, frac& add);
c++
Comments
Post a Comment