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;
};
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;
}