Hey guys, I am working on a fraction program where you can add, divide, subtract, multiply, and divide and it uses addressing. Anyway I got it to work except when I added an error message if to make sure that the 2nd Numerator, 1st and 2nd Denominator are not zero. However it displays the error message even if none of the values are zero. What am I doing wrong? Thanks!

#include <iostream>
using namespace std;

void getFrac(int& numerator, int& denominator);
void readFracProblem(int& num1, int& denom1, int& num2, int& denom2, char& op);
void addFrac( int num1, int denom1, int num2, int denom2, int& num3, int& denom3);
void subFrac( int num1, int denom1, int num2, int denom2, int& num3, int& denom3);
void multiFrac( int num1, int denom1, int num2, int denom2, int& num3, int& denom3);
void divFrac( int num1, int denom1, int num2, int denom2, int& num3, int& denom3);


int main()
{
  
  int num1, denom1, num2, denom2, num3, denom3;
  char op;
  
  cout << "Please enter 1st & 2nd fraction with slash and either add, " <<endl;
  cout << "subtract, multiply. " << endl;
  
  readFracProblem (num1, denom1, num2, denom2, op);
  
 if (num2 || denom1 || denom2 == 0)
 cout << "cannot do problem!" << endl;
 
 else
  
  if (op = '+')
      { 
        addFrac(num1, denom1, num2, denom2, num3, denom3);
        cout << "Answer is: " <<  num3 << "/" << denom3;
      }     
  
  else
  if (op = '-')
  { 
      subFrac(num1, denom1, num2, denom2, num3, denom3);
      cout << "Answer is: " <<  num3 << "/" << denom3;
  }     
  
  else
  if (op = 'x', 'X', '*')
  { 
      multiFrac(num1, denom1, num2, denom2, num3, denom3);
      cout << "Answer is: " <<  num3 << "/" << denom3;
  }     
  
  else
  if (op = '/')
  { 
    divFrac(num1, denom1, num2, denom2, num3, denom3);
    cout << "Answer is: " <<  num3 << "/" << denom3;
  }     
  
  else
  cout << "Operation is invalid!" << endl;


system ("pause");

return 0;


}




void getFrac(int& numerator, int& denominator)

{ 
    char slash;
    cin >> numerator >> slash >> denominator;
}

void readFracProblem(int& num1, int& denom1, int& num2, int& denom2, char& op)

{
    getFrac(num1, denom1);
    cin >> op;
    getFrac(num2, denom2);
}    
    
void addFrac( int num1, int denom1, int num2, int denom2, int& num3, int& denom3)

{
    num3 = num1 * denom2 + num2 * denom1;
    denom3 = denom1 * denom2;
    
}    

void subFrac( int num1, int denom1, int num2, int denom2, int& num3, int& denom3)

{
    num3 = num1 * denom2 - num2 * denom1;
    denom3 = denom1 * denom2;
}    


void multiFrac( int num1, int denom1, int num2, int denom2, int& num3, int& denom3)

{
    num1 * num2;
    denom1 * denom2;
    
}

void divFrac( int num1, int denom1, int num2, int denom2, int& num3, int& denom3)

{
    num1 * denom2;
    denom1 * num2;
}

Try changing this line:

if (num2 || denom1 || denom2 == 0)

to this:

if (num2 == 0 || denom1 == 0 || denom2 == 0)
commented: Great help! +1

why would you care if the numerator is zero? are we no longer allowed to add or muliply zero?

it seems i missed the memo ... again.

:(

Thanks a million!

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.