Hello, I'm currently writing a program that is supposed to add, subtract, divide, and multiply fractions. I've been tinkering with it all day and I just can't seem to get it to work correctly. Right now I'm getting a floating point exception and I have no idea where to begin to fix it. Sorry if this is an easy problem, I'm new to programming.
This is a sample of how the program should look when it's compiled and ran correctly:
Sample compilation and execution
This is the sample input file I am going to be redirecting from:
3/5 + 1/5
12/10 - 1/3
-42/5*1/2
7/4 / 0/5
11/2 + 6/4
This is the expected output:
3/5 + 1/5 = 20/25
12/10 - 1/3 = 26/30
-42/5 * 1/2 = -42/10 = -4 2/10
7/4 / 0/5 could not be solved because division by zero is not defined
11/2 + 6/4 = 56/8 = 7

#include <iostream>

using namespace std;
int main()
{
  int num1, num2; // numerator                                                                                                                       
  int den1, den2; // denominator                                                                                                                     
  int frac1, frac2; // whole fractions                                                                                                               

  den1 > 0;
  den2 > 0;
  frac1 = num1/den1;
  frac2 = num2/den2;

  cin >> frac1 >> frac2;
  if (frac1+frac2)
    cout << num1 << "/" << den1 << " + " << num2 << "/" << den2 << " = " << ((num1*den2)+(den1*num2)) << "/" << (den1*den2) << endl;
  else
    if (frac1-frac2)
      cout << num1 << "/" << den1 << " - " << num2 << "/" << den2 << " = " << ((num1*den2)-(den1*num2)) << "/ " << (den1*den2) << endl;
    else
      if (frac1*frac2)
          cout << num1 << "/" << den1 << " * " << num2 << "/" << den2 << " = " << (num1*num2) << "/ " << (den1*den2) << endl;
        else
          if (frac1/frac2)
            cout << num1 << "/" << den1 << " * " << num2 << "/" << den2 << " = " << (num1*den2) << "/ " << (den1*num2) << endl;
 return 0;
 }

Recommended Answers

All 3 Replies

Lines 10 and 11 do nothing.

Lines 12 and 13, you are doing integer division of integers that are as yet uninitialized.

Then in 15 you do input to the variables that should hold the resulting fractional value.

Your if statements make no sense.

Where you should be going is reading in the operations ( 3/4 + 2/9, for example) in a way that separates the numbers from the symbols. The /s can be discarded, but you'll need the operator.

With two numerators and two denominators and an operator, you can then start to do the work.

Branch on the operator, you'll handle numbers differently depending on the operation to be perfomed.

You then need to do the various operations the same way you would by hand. For addition and subtraction, find least common denominator, convert the numerators and denominators as needed. Add or subtract numerators.

Multiply and divide will be easier.

Once you have a result, be sure to convert it to its properly reduced form.

#include <iostream>

using namespace std;
int main()
{
  int num1, num2; // numerator                                                                                                                       
  int den1, den2; // denominator                                                                                                                     

  cin >> num1 >> den1 >> num2 >> den2;

  if ((num1/den1)+(num2/den2))
    cout << num1 << "/" << den1 << " + " << num2 << "/" << den2 << " = " << ((num1*den2)+(den1*num2)) << "/" << (den1*den2) << endl;
  else
      if ((num1/den1)-(num2/den2))
      cout << num1 << "/" << den1 << " - " << num2 << "/" << den2 << " = " << ((num1*den2)-(den1*num2)) << "/ " << (den1*den2) << endl;
    else
      if ((num1/den1)*(num2/den2))
          cout << num1 << "/" << den1 << " * " << num2 << "/" << den2 << " = " << (num1*num2) << "/ " << (den1*den2) << endl;
        else
          if ((num1/den1)/(num2/den2))
            cout << num1 << "/" << den1 << " * " << num2 << "/" << den2 << " = " << (num1*den2) << "/ " << (den1*num2) << endl;
 return 0;
 }

Thanks for the reply vmanes. So I took out lines 10 and 11 and the frac int values all together, so now I'm only dealing with num1, num2, den1, and den2. In my output I did separate the int values from the "/" like you said. However, Im still getting a floating point exception. I don't see any problems with my output statements. So I'm assuming it's my if statements that are incorrect? Sorry about my total cluelessness haha.

Oh yeah I forgot to mention that my instructor specified not to look for the LCD and that we are not required to reduce the final fraction. So that makes it easier I guess.

Consider your first input sample:
3/5 + 1/5
Will line 9 correctly read this?
No.
Your input will have to be a bit more sophisticated to take in an integer, toss away the /, read an integer, take the operator as a character, read an integer, toss away the /, read the final integer.

Then you will have the two numerators and the two denomonators, and an operator.

When it comes to your branching, make the decision based only on the operator, not the result of doing operations. How would your line 11 get around to doing the addition given the input above?
if( 3/5 + 1/5 ) //is this true or false?

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.