#include <cstdlib>
#include <iostream>

using namespace std;


class fractions

{
      private:
              int numerator1, denominator1, numerator2, denominator2;
      public:
              fractions(int = 0, int = 1, int = 0, int = 1);
              void read_fractions ();
              void multiply_fractions ();
              void add_fractions ();
              double add_fractions (double);
              double multiply_fractions (double);
};
void fractions::read_fractions ()
{
     cout << " Enter numerator and denominator for first fraction." << endl;
     cin >> numerator1 >> denominator1;
     cout << " Enter numerator and denominator for the second fraction." << endl;
     cin >> numerator2 >> denominator2;
     cout << "Your fractions are  " << numerator1 "/" << denominator1 ;
     cout << "and" << numerator2 "/" << denominator2 << endl;
      
     
}
void fractions::add_fractions 
{
       double sum_result;
       sum_result = ((numerator1*denominator2)+(numerator2*denominator1))
                    /denominator1*denominator2;
      
       return (sum_result);
}
void fractions::multiply_fractions
{
       double product;
       product = (numerator1*numerator2)/(denominator1*denominator2);
       
       return (product);
}

int main()
{   double sum, product;
    sum = sum_result;
    cout << "The sum of the two fractions are  " << sum << endl;
    product = product_result;
    cout << "The product of the two fractions are " << product << endl;
    
      
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Lot's of problems here and I can't seem to solve one.
First in line 26 the error is "expected ';' before string constant"
second on line 32 the error is " invalid function declaration" (also on line 40)
There are more so if anyone wants to chime in and help out with whatever you may see all help is appreciated.

Recommended Answers

All 5 Replies

You're missing << in line 27 and () in lines 32 and 40.

Thank you so much, I was able to fix those problems. And now of course I have new ones.
First, my assignment says to 1. Use member functions to enter two fractions. ( I think I have done that). 2. Display the two fractions. (I know I have done that).
3. Use the member function to multiply two fractions and another member function to print the result. 4. Use the member function to add two fractions and another to print the result. (I am so bad at this I don't even know if I'm on the right track). Here is my new code:

#include <cstdlib>
#include <iostream>

using namespace std;


class fractions

{
      private:
              int numerator1, denominator1, numerator2, denominator2;
      public:
              fractions(int = 0, int = 1, int = 0, int = 1);
              void read_fractions ();
              
              
              double add_fractions (double);
              double multiply_fractions (double);
};
void fractions::read_fractions ()
{
     cout << " Enter numerator and denominator for first fraction." << endl;
     cin >> numerator1 >> denominator1;
     cout << " Enter numerator and denominator for the second fraction." << endl;
     cin >> numerator2 >> denominator2;
     cout << "Your fractions are  " << numerator1 << "/" << denominator1 << endl;
     cout << "and" << numerator2 << "/" << denominator2 << endl;
      
     
}
double fractions::add_fractions ()
{
       double sum_result;
       sum_result = ((numerator1*denominator2)+(numerator2*denominator1))
                    /denominator1*denominator2;
      
       return (sum_result);
}
double fractions::multiply_fractions ()
{
       double product;
       product = (numerator1*numerator2)/(denominator1*denominator2);
       
       return (product);
}

int main()
{  
    double sum, product, sum_result, product_result;
    sum = sum_result;
    cout << "The sum of the two fractions are  " << sum << endl;
    product = product_result;
    cout << "The product of the two fractions are " << product << endl;
    
      
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

The errors I am now getting are:
line 32 prototype for 'double fractions::add_fractions()does not match any in class 'fractions' But it looks like I have in there.
line 17 double fractions::add_fractions (double)
I am getting the exact same error for my other function "multiply_frations"

I know it seems like I may be asking for people to "do" my homework for me, but I'm really not. I am dying to understand this stuff but I'm having a heck of a time doing so. All of your help is so very appreciated.

Line 17 says that the add_fractions function has a parameter of type double. Line 31 says that add_fractions has no parameters. These two lines are not consistent with each other, so the compiler is complaining.

Again thank you guys so much for the help. I finally have an error free running program, however, the results are way off. ie: if I try to add the two fractions 1/2+1/2 I get some crazy result 1.42123e-313. This tells me that there may be a problem as simple as a variable declaration but I can't seem to find it. Here is my revised code:

#include <cstdlib>
#include <iostream>

using namespace std;


class fractions     //Define the class fractions

{
      private:      //
              int numerator1, denominator1, numerator2, denominator2;
      public:
            
              void read_fractions ();       //Member function declarations
              
              
              double add_fractions (double);
              double multiply_fractions (double);
};
void fractions::read_fractions ()       //Member funtions definition
{
     cout << " Enter numerator and denominator for first fraction." << endl;
     cin >> numerator1 >> denominator1;
     cout << " Enter numerator and denominator for the second fraction." << endl;
     cin >> numerator2 >> denominator2;
     cout << "Your fractions are  " << numerator1 << "/" << denominator1 << endl;
     cout << "and" << numerator2 << "/" << denominator2 << endl;
      
     
}
double fractions::add_fractions (double sum_result)    //Member function definition
{
      
       sum_result = ((numerator1*denominator2)+(numerator2*denominator1))
                    /denominator1*denominator2;
      
       return (sum_result);
}
double fractions::multiply_fractions (double product_result)             //Member funcition definition
{
       
       product_result = (numerator1*numerator2)/(denominator1*denominator2);
       
       return (product_result);
}

int main()
{  
    double sum, product, sum_result, product_result;                        
    fractions f1;        //Object in class fraction defined
    f1.read_fractions(); //Call member function for object f1

    sum = sum_result;    //call member function
    cout << "The sum of the two fractions are  " << sum << endl;
    product = product_result;   //call member function
    cout << "The product of the two fractions are " << product << endl;
    
      
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

At line 35, you need parentheses to enclose "denominator1*denominator2" like you did at like 42.

As for the main problem, lines 53 to 56 are very wrong. There is absolutely no relationship between sum_result in main() and sum_result in add_fractions(). These are two completely unrelated variables. The fact that they have the same name does not make them have the same value. In any case, the function add_fractions and multiply_fractions are never called anywhere, why would you even expect that the sum or product has even been computed anywhere. To fix the problem, you could do this:

sum = f1.add_fractions(sum_result);    //call member function
    cout << "The sum of the two fractions are  " << sum << endl;
    product = f1.multiply_fractions(product_result);   //call member function
    cout << "The product of the two fractions are " << product << endl;

But you should also realize that the parameters to the function add_fractions and multiply_fractions are useless (they are input parameters without meaningful values). You should make those two functions to not take any parameter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.