Class constructor error check / prohibit value problem

Hi, I am new to programming & I was wondering if any one could help me. I have a class that deals with fractions (entered as two int's). I am having trouble with the constructor that sets the fraction to any value. I need to put an error check (or get the constructor to prohibit 0) for the denominator value. I based my code on other pice of code from the net (that worked) but for some reason it is just not working for my program.

When I run the code the "error check" is just ignored. I hope I am not posting too much code here.

#include <iostream>
#include <cmath>
#include <cassert>
using namespace std;
class Fraction
{
public:
Fraction();
Fraction(int temp_numerator, int temp_denominator);
Fraction(int temp_whole_number);
void setNumerator(int numerator);
void setDenominator(int denominator);
int getNumerator();
int getDenominator();
friend Fraction operator +(const Fraction& newFraction1, const Fraction& newFraction2);
void input (istream& ins);
void output (ostream& outs);
private:
int numerator;
int denominator;
};

int main()
{
Fraction WholeNumber(77);
Fraction NextFraction(1,2);
Fraction NewFraction, AddFraction, FractionOne;
NewFraction.setNumerator(2);
NewFraction.setDenominator(0);

cout << "Test constructor with 2 arguments (NewFraction & NextFraction): " << endl;
cout << "-------------------------------------------------------------- \n";
cout << endl;
cout << "NEW FRACTION:";
NewFraction.output(cout);
cout << endl;
cout << "NEXT FRACTION:";
NextFraction.output(cout);
cout << endl;

Fraction WholeNo(99);
WholeNo.output(cout);

//Tests the + overload friend function
cout << "Adding NewFraction & NextFraction." << endl;
cout << "-------------------------------- \n";
AddFraction = NewFraction + NextFraction;
AddFraction.output(cout);
cout << endl;

cout << endl;
cout << "Test the input and output functions: " << endl;
cout << endl;
FractionOne.input(cin);
FractionOne.output(cout);


return 0;
}

//constructer to initialize a fraction to 0 - 0/1
Fraction::Fraction()
//Fraction::Fraction():numerator(0), denominator(1)
{
numerator = 0;
denominator = 1;
}

//Constructer to set both parts to any value
Fraction::Fraction(int temp_numerator, int temp_denominator)
{
numerator = temp_numerator;
assert(temp_denominator >0);
if (temp_denominator == 0)
cout<<"Eror"<<endl;
else
denominator = temp_denominator;
}


//Constructer to set the number to a whole number - one argument
//Fraction::Fraction(int whole_number): numerator(whole_number), denominator(1)
Fraction::Fraction(int temp_whole_number)
{
numerator = temp_whole_number;
denominator = 1;
}

//Mutators
void Fraction :: setNumerator(int temp_numerator)
{
numerator = temp_numerator;
}

void Fraction :: setDenominator(int temp_denominator)
{
denominator = temp_denominator;
}

//Accessors
int Fraction :: getNumerator()
{
return(numerator);
}

int Fraction :: getDenominator()
{
return(denominator);
}

//overloads + as a friend function
Fraction operator +(const Fraction& the_object1, const Fraction& the_object2)
{
Fraction temp;
temp.numerator = ((the_object1.numerator * the_object2.denominator) +
the_object2.numerator * the_object1.denominator);

temp.denominator = (the_object1.denominator * the_object2.denominator);
return temp;
}
//INPUT
//uses iostream
//uses string
void Fraction :: input(istream& ins)
{
cout << "Please enter the numerator: ";
ins >> numerator;

cout << "Please enter the denominator: ";
ins >> denominator;
}

//OUTPUT
//uses iostream
void Fraction :: output (ostream& outs)
{
outs << endl;
//outs << "The fraction's details are:" << endl;
//outs << endl;
outs << "Fraction: " << numerator << "/" << denominator << endl;
outs << "Numerator: " << numerator << endl;
outs << "Denominator: " << denominator << endl;
}

The output I get is:

Test constructor with 2 arguments (NewFraction & NextFraction):
--------------------------------------------------------------

NEW FRACTION:
Fraction: 2/0
Numerator: 2
Denominator: 0

NEXT FRACTION:
Fraction: 1/2
Numerator: 1
Denominator: 2


Fraction: 99/1
Numerator: 99
Denominator: 1
Adding NewFraction & NextFraction.
--------------------------------

Fraction: 4/0
Numerator: 4
Denominator: 0


Test the input and output functions:

Please enter the numerator: 9
Please enter the denominator: 0

Fraction: 9/0
Numerator: 9
Denominator: 0



Last edited on
The typical approach is to throw an exception if the denominator is zero.

1
2
3
4
5
Fraction::Fraction( int n, int d ) : numerator( n ), denominator( d )
{
    if( d == 0 )
        throw std::range_error( "denominator must not be zero" );
}


Thank you for your help - I will try it out
Topic archived. No new replies allowed.