Problem with readReaction function for a whole number

I have an assignment to create a program that reads 2 fractions and print their product. I need to have 3 constructors, one defaults, one that allows us to create a fraction that represents a whole number, and the third constructor should allow us to specify the numerator and denominator of a new fraction object.
I have to test the function with the fractions 3/4 and 5/6 and also with 1/2 and 1.
I can't make the readFraction function take the number one, without the slash and a denominator.
My prof. suggested that I should use:
Fraction(const int &num){ myNumerator = num;myDenominator = 1; } //defines the fraction as a whole number
But I couldn't make it work. So I think that the readFraction function is the problem.
Here is my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include<iostream>
#ifndef FRACTION_H   
#define FRACTION_H

using namespace std;

class Fraction
{
public:
	Fraction();
	Fraction(const int);
	Fraction(int, int);
	void setNum(int);
	void setDen(int);
	int getNum() const;
	int getDen() const;
	void readFraction();
	void print() const;
	Fraction multiply(Fraction frct);
private:
	int num, den;
};

#endif  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  #include "fraction.h"
#include <iostream>
using namespace std;

Fraction::Fraction()
{
	setNum(0);
	setDen(1);
}
Fraction::Fraction(int n)
{ 
	num = n;
	den = 1; 
}

Fraction::Fraction(int n, int d)
{
	setNum(n);
	setDen(d);	
}
void Fraction::setNum(int n)
{
	num = n;
}
void Fraction::setDen(int d) 	 
{  	if( d == 0 )
    {cout<<"The denominator cannot be zero!";
    exit(0);}
    else
    den = d;
}
int Fraction::getNum() const
	{ return num; }
int Fraction::getDen() const
	{ return den; }
Fraction Fraction::multiply(Fraction frct)
{
	Fraction frct1( num*frct.num , den*frct.den );
	return frct1;
}
void Fraction::readFraction()
{
	char slash = '/';
	{
		cin >> num >> slash >> den;
	
	}
	
}
void Fraction::print() const
{
	cout << num << "/" << den;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include "fraction.h"
#include "fraction.cpp"
#include <iostream>
using namespace std;

int main()
{

	Fraction f1, f2, f3;
		
	cout << "Enter two fractions, one at a time, in the form \"numerator/denominator\"" << endl;
	cout << "Enter the first fraction : " << endl;
	f1.readFraction();
	cout << "Enter second fraction: " << endl;
	f2.readFraction();
	f3 = f1.multiply(f2);
	f1.print();
	cout << " * ";
	f2.print();
	cout << " = ";
	f3.print();
	cout << endl;
	
	f3 = f1.multiply(f2);
	f1.print();
	cout << " * ";
	f2.print();
	cout << " = ";
	f3.print();
	cout << endl;
	
}
Last edited on
First, you should not be using
#include "fraction.cpp"
You should not have to include .cpp files.
Second, why don't you use inheritance and make a parent class Fraction and subclasses NoNumeratorFrac and WholeNumFrac.
That would also help you with OOP.
I put
#include "fraction.cpp"
because I need to fix the settings of my compiler and I don't know how. I will get to it when I have a chance.
Can you please explain a little more in detail what you mean with the two subclasses. Why isn't there a normal fraction subclass. And how the compiler will know which class to use, when the user enters the fraction? The user may enter a regular fraction or a whole number. The different subclasses will allow me to have different readFraction() fuctions, but how can I tell the compiler to use one class if the user enters a regular fraction, or another class if the y enter a whole number?
Well, you can tell which type of fraction you are using at compile time. You are initializing three fractions yourself, so you know which class to use. I guess that you are trying to have users enter their own fractions. That would be harder. So maybe you shouldn't have subclasses for such a small program. Maybe you are not entering the slash when you enter the fraction and the program does not "see" the slash? Just make sure you type the slash at the command line.
Does that help?
I tried, but the program will not show the results, if I don't type a slash and another number, because this is how my readFunction is. I was wondering, can I add something to the construcor or the readFunction that could help?
Topic archived. No new replies allowed.