I'm writing a program for a class that does operations with rational numbers. I'm not finished with it, but right now i'm working on the is_negative() function which is required to be in the program by the teacher. He has set up a make file to test each part of the program and i have gotten the through the tests to make the constructors work properly. Now i'm on the is_negative test but for some reason is will only work for objects that are initialized to something without a 1 in the denominator. So something like rational rat(3,2), rat1(5,4) will work. The problem comes when an uninitialized obj such as rational rat, rational rat(2) and rat2(2,1) is used. For some reason the the is_negative function is returning 204 for the neg variable when it is a boolean variable and should return 0 or 1. If someone could help i would really appreciate it. Keep in mind i'm not finished so i just need help with the is_negative function. Thank you.

``````#include <iostream>
#include <string>
#include <cmath>

using namespace std;

class Rational{
public:
int num;
int den;
bool neg;

Rational(int n, int d); // contructor for rationals
Rational(int n); // contructor for reals
Rational();//default constructor

int get_num();
int get_denom();
int gcd();
void simplify();
bool is_negative();
Rational operator +(Rational);
Rational operator -(Rational);
Rational operator *(Rational);
Rational operator /(Rational);

};

int main(){
Rational result, a, rat(2,1), rat2(0), rat3(6,-3), rat4(-3,-8);

result.den = rat2.get_denom();
result.num = rat2.get_num();
result.neg = rat2.is_negative();

cout << result.num << "/" << result.den << endl;
cout << result.neg << endl;

}

Rational::Rational(int n, int d)
{
if(n == 0)
{
num = n;
den = 1;
}else
{
num = n;
den = d;
}
}
Rational::Rational(int n)
{
num = n;
den = 1;
}
Rational::Rational()
{
num = 0;
den = 1;
}
int Rational::get_num()
{
simplify();
return abs(num);

}
int Rational::get_denom()
{
simplify();
return abs(den);

}
int Rational::gcd()
{	int a = num;
int b = den;
int temp;

while(b != 0)
{
temp = b;
b = a % b;
a = temp;
}
return a;
}
void Rational::simplify()
{
int gcdNum = gcd();

if(gcdNum != 0)
{
num = num / gcdNum;

den = den / gcdNum;
}

}
bool Rational::is_negative()
{

if(den == 1)
{
if(num < 0)
{
neg = true;
}
}
else{
if(num||den < 0)
neg = true;
else
neg = false;
}

return neg;
}

Rational Rational:: operator +(Rational ratPassed)
{
Rational ratResult;
ratResult.num = num * ratPassed.den + den * ratPassed.num;
ratResult.den = den * ratPassed.den;

return ratResult;

}
Rational Rational:: operator -(Rational ratPassed)
{
Rational ratResult;
ratResult.num = num * ratPassed.den - den * ratPassed.num;
ratResult.den = den * ratPassed.den;

return ratResult;
}
Rational Rational:: operator *(Rational ratPassed)
{
Rational ratResult;
ratResult.num = num * ratPassed.num;
ratResult.den = den * ratPassed.den;

return ratResult;
}
Rational Rational:: operator /(Rational ratPassed)
{
Rational ratResult;
ratResult.num = num * ratPassed.den;
ratResult.den = den * ratPassed.num;

return ratResult;
}``````

why is it checking to see if den == 1? What does that have to do with whether its negative or not? `if(num||den < 0)` That line is wrong. Should be `if(num < 0 ||den < 0)` . The previous if statement on lines 112-119 is meaningless.

Don't be rude. Most posters are beginning programmers, or have had a long night.

## All 6 Replies

why is it checking to see if den == 1? What does that have to do with whether its negative or not? `if(num||den < 0)` That line is wrong. Should be `if(num < 0 ||den < 0)` . The previous if statement on lines 112-119 is meaningless.

Oh wow i feel really stupid, i don't know how i missed that.

Don't be rude. Most posters are beginning programmers, or have had a long night.

commented: darn skippy +2

Don't be rude. Most posters are beginning programmers, or have had a long night.

??? I'm glad you know what that means. It just looks like spam to me... I'm tempted to report it for not being written in English.

??? I'm glad you know what that means. It just looks like spam to me... I'm tempted to report it for not being written in English.

`这么明显的错误你都发现不了？你到底是不是C++编程人员` is in Chinese and maps to `Such an obvious mistake you can not find it? You in the end is not C + + programmers` in english.

`这么明显的错误你都发现不了？你到底是不是C++编程人员` is in Chinese and maps to `Such an obvious mistake you can not find it? You in the end is not C + + programmers` in english.

Wow, brutal. :@:(

Be a part of the DaniWeb community

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