Adding a series of fractions

+2 Patricia Ghann · January 6, 2015
Hello Bucky, Happy New Year. Please I want to write a program in c++ to add a series of fractions and am finding it difficult so I need your help please. I want to add for example 


Post a Reply


- page 2
Oldest  Newest  Rating
0 Number Double07 · January 7, 2015
It would be easier if you requested the user to input the numerator, then the denominator.  I tried the fraction input method using your code, but I'm not sure what is going on there myself.  It's more complicated, it can probably be done, but I don't know how to do it.  Also, both requests (cout statements of numerator and denominator)  need to be inside the while loop so it knows to end when it user inputs the -1 value.
0 c student · January 7, 2015
if it's not too late, here is my functioning code written in C since i dont code in c++ however should be easy to translate across.  i have added comments to show psuedo code for better understanding:
#include <stdio.h>
#include <stdlib.h>

// define a struct for a fraction type to hold
// both numerator and denominator
typedef struct {
    int num;
    int den;
} fraction;

fraction fracAdd (fraction, fraction);

int main (void) {

    // request amount of fractions
    puts ("Please enter the amount of fractions");
    // acquire number of fractions to be calculated
    int amount = getchar()-48;
    // declare array of fraction types according to the previous line
    fraction frac[amount];
    // declare a resultant fraction
    fraction result;

    // loop to enter in each fraction
    for (int n = 0; n < amount; n++) {
        // request numerator for fraction n
        printf ("Please enter in the numerator for fraction %d\n", n+1);
        // input into fraction array n for numerator member
        scanf ("%d", &frac[n].num);
        // request denominator for fraction n
        printf ("Please enter in the denominator for fraction %d\n", n+1);
        // input into fraction array n for denominator member
        scanf ("%d", &frac[n].den);
if (frac[n].den == 0) {
fprintf (stderr, "Zero is not a valid denominator value");
exit (1);
        // this is just to place the first fraction into result
        if (n == 0) {
            result = frac[n];
        // then for the rest of the fractions, add them onto the result
        } else {
            result = fracAdd (result, frac[n]);

    // print result with numerator and denominator
    printf ("Result: %d/%d\n", result.num, result.den);

    return 0;


// function to add fractions
fraction fracAdd (fraction a, fraction b) {
    // declare resultant fraction
    fraction result;
    // adding numerators
    result.num = a.num*b.den+b.num*a.den;
    // adding common denominator
    result.den = a.den*b.den;

    // return resultant
    return result;

good luck
note: does not simplify fractions.  if you wish to simplify the fractions, just make a function to calculate gcd (i think).
0 Number Double07 · January 7, 2015
I'm trying to work it out but I'm having a problem.  It's giving me an answer more than the answer I want. 
0 Number Double07 · January 7, 2015
using namespace std;

int main()

{float num = 0;
float den = 0;
int numberOfFractionEntered = 0;
float fraction = 0;
float sum = 0;

while (num !=-1){

cout << "Enter numerator or enter -1 to quit: " <<endl;
cin >> num;

cout << "Enter denominator or enter -1 to quit " <<endl;
cin >> den;

numberOfFractionEntered ++;

fraction = (num / den);

sum = fraction + sum;

cout << "The sum of of the fractions entered is " << sum  << endl;

return 0;


It keeps giving me an answer more than what I want.  Like, I enter .5 + .5 +.5, and I get 2.5.
0 Number Double07 · January 7, 2015
Oh, I see what's happening in my code.  It's adding up the -1 inputs for my numerator and denominator before it ends the while loop.  So every answer I get is 1 greater than the actual answer.  I could just subtract 1 from the sum I guess when I cout it.  However, is there another way to prevent the loop from adding that "trip" values of -1?
0 Number Double07 · January 7, 2015
Nice, that works!  Thanks man.
-1 Patricia Ghann · January 6, 2015
Lol thanks
  • 1
  • 2



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator