-6

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!

  • 1
    Note: += is not =+ . I don't have right now to read everything. – Lasoloz Nov 29 '15 at 09:26
  • 2
    Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). You have a lot of code that is not relevant to the core problem. – R Sahu Nov 29 '15 at 09:29
  • There is too much that is wrong with this code. Try solving one problem at a time. – juanchopanza Nov 29 '15 at 09:52
  • 1
    It will become easier for you to spot typos like "=+" instead of "+=" yourself, if you make it a habit to layout your code in a meaningful way, e.g. use a **consistent** indentation style, and don't place empty lines in a random fashion. – decltype_auto Nov 29 '15 at 09:54
  • @RSahu Thanks, I know its a lot I just wasnt sure if the problem was with the print function or the friend function but will post again with both – Connor Larson Nov 29 '15 at 10:09

1 Answers1

0

i think your problem is in these lines:

fractot[0].integral =+ fracarry[j].integral;
fractot[0].numerator =+ fracarry[j].numerator;

in order to summing a parameter with its own value you must write it this way:+=

pouyan021
  • 177
  • 1
  • 4
  • 19
  • That's one problem, but there's more. Like `1/2 + 1/3` not being `2/5`. – Bo Persson Nov 29 '15 at 09:38
  • @BoPersson i think it returns to junior math,not the code :) – pouyan021 Nov 29 '15 at 09:49
  • @BoPersson Yes, I already stated that at the beginning, I just want to work out how to sum them directly then move on to (yb + az)/z*b in the definition of the friend function. also Thanks but I tried that coding too, it just wants to return the last input fraction, there is no summing happening as far as I can tell. Is there a problem arising from the way the friend function is defined? – Connor Larson Nov 29 '15 at 10:04
  • I'm a little confused that when you define a function, you are putting a specific object of type fraction, is the name you put (eg for my code 'fracarry' arbitrary and interchangeable outside of the definition? thanks – Connor Larson Nov 29 '15 at 10:05
  • @ConnorLarson i can solve whatever i think is wrong with the above code,sorry but i can't logically fix your code,if you want to sum something with it's own previous value you should use syntax `+=` and not `=+` and i can't get your point by defining your `fraction` function like that,what do you mean by friend function? why didn't you just defined it the common way? – pouyan021 Nov 29 '15 at 10:14
  • @pouyankhodabakhsh Oh no, I'm not asking you to fix the logic in the math at all, Just want it so that I am summing the different values of a dynamic array together of each different variable type inside the class and putting them into a new, originally empty class of the same type (function). The friend function is because the integer, numerator and denominator variables are private. Is it not necessary? thanks – Connor Larson Nov 29 '15 at 10:23
  • @ConnorLarson i see only one class in your code above and no it's not necessary to define your function that way,because every function which has been defined inside the class can use even the private integers of that class anytime,i suggest instead of `fractot` you just define an integer `sum` then rewrite the code like `sum+=fracarry[j].whatever` – pouyan021 Nov 29 '15 at 10:32
  • For the assignment it is required to use a friend function as above, I have edited to remove the logical error. It worked once and printed the value needed however then it continued to compile but would quit before printing the final sum. Are there any other obvious errors you can see? – Connor Larson Nov 29 '15 at 11:01
  • @pouyankhodabakhsh Thanks – Connor Larson Nov 29 '15 at 11:02
  • @ConnorLarson there's a lot of enhancements that can be performed with your code i suggest you take some online courses in c++ [here](http://www.infiniteskills.com) – pouyan021 Nov 29 '15 at 11:16