I've written a code which takes an input of 3 integers (integer, numerator and denominator) and displays them as "a{b/c}" I have defined/declared a class (fraction) which contains these 3 separate integer variables and the functions to read and print them. In order to store several of these different outputs, I've created a dynamic array of the class type defined. I would like to sum all the values together eg fract[0].a + frac[1].a .. etc for the integer part and find the sum of the fraction part as well and then display this.
for the class declaration I have included a friend function:
friend fraction operator +(fraction, fraction);
I have also declared 2 dynamic arrays of type fraction
fraction* fracarry = new fraction[x];
fraction* fractot = new fraction[1];
fracarry stores the input fractions, fractot is initialized as:
numerator = 0;
denominator = 1;
integral = 0;
The definition of the friend function is as follows:
fraction operator+(fraction, fraction)
{
for(int i = 0; i < x; i++)
{
fractot[0].denominator += fracarry[i].denominator*fractot[0].denominator;
fractot[0].integral += fracarry[i].integral;
fractot[0].numerator += ((fracarry[i].numerator*fractot[0].denominator) + (fractot[0].numerator*fracarry[i].denominator));
}
return fractot[0];
}
where x is an input from the user for how many fractions will be entered.
The main function is:
cout << " Please input how many fractions you will be inputting: " << endl;
cin >> x;
fraction *fracarry = new fraction[x];
fraction *fractot = new fraction[1];
fracarry[0].read();
fractot[0] = fractot[0] + fracarry[0];
fractot[0].print2();
I'm not sure if I should have the loop in the function definition or the main function. It complies and the read/print functions work but I can't get it to display fractot.print2() as the final sum of the values stored in fracarry
I don't want exact code, just any hints of where I may have gone wrong/what to look up, or any tutorials that might help etc
Thanks!
**edit This is the same question as asked but I tried to cut down the irrelevant code/ make it more logical as requested!