Class functions problem

I'm writing a program to read in fractions, from user input, and then have them select an option on how to calculate them. I'm running into an error where inside of my CFrac::add function it doesn't recognize numerator and denominator as modifiable values (Microsoft VS 2012). Did I miss a step in referencing them? I'm new to classes I was hoping someone could direct me to what I'm doing wrong here. Ignore the class functions after add, I haven't worked them in yet. Thanks for the feedback.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>

using namespace std;
//class definition
class CFrac
{
private:
	int numerator, denominator;
	void simplify();

public:
	CFrac (int = 0, int = 1);
	~CFrac();
	int add (const CFrac&) const;
	int subtract (const CFrac&) const;
	int multiply (const CFrac&) const;
	int divide (const CFrac&) const;
	void showFrac () const;
	void setFrac ();
};
//start of class function definition
void CFrac::simplify ()
{
	int num, denom, mod, remainder;

	num = numerator;
	denom = denominator;

	while(mod != 0)
	{
		mod = num / denom;
		remainder = num % denom;
		num = denom;
		denom = remainder;
	}

	numerator /= remainder;
	denominator /= remainder;
}

CFrac::~CFrac() {}

int CFrac::add (const CFrac& fraction) const
{
	numerator = numerator * fraction.denominator + fraction.numerator * denominator;

	denominator = denominator * fraction.denominator;

	return numerator, denominator;
}

int CFrac::subtract (const CFrac& fraction) const
{

}

int CFrac::multiply (const CFrac& fraction) const
{

}

int CFrac::divide (const CFrac& fraction) const
{

}

void CFrac::showFrac () const
{

}

void CFrac::setFrac ()
{
	cout << "Enter a fraction: ";
	cin >> numerator;
	cin.ignore(1);
	cin >> denominator;
}
//end of class function definition

int userMenu (int);

int main()
{
	int menuChoice;

	CFrac fracOne, fracTwo;

	while(menuChoice != 5)
	{
		if(menuChoice == 1)
			fracOne.add(fracTwo);
		else if(menuChoice == 2)
			fracOne.subtract(fracTwo);
		else if(menuChoice == 3)
			fracOne.multiply(fracTwo);
		else if(menuChoice == 4)
			fracOne.divide(fracTwo);
		else
			cout << "Not a valid option, select again." << endl;
	}

	_getch();
	return 0;
}

int userMenu(int choice)
{
	cout << "Fraction Calculation Menu:" << endl
		 << "1 -- ADDITION" << endl
		 << "2 -- SUBTRACTION" << endl
		 << "3 -- MULTIPLICATION" << endl
		 << "4 -- DIVISION" << endl
		 << "5 -- EXIT" << endl
		 << "Enter a choice --> ";

	cin >> choice;

	return choice;
}
Seems like you don't quite understand the meaning of const.

int add (const CFrac&) const;
const inside the parentheses means the variable CFrac is not allowed to be changed
const at the end of the method declaration means this method is not allowed to change any class fields
=> numerator and denominator are not allowed to be changed inside the method
Last edited on
That makes sense now that you call it out.

So in using these functions to do the calculations how should I go about passing the results down the chain?

IE: If the user selects addition, and I add my two sets of fractions together inside of CFrac::add, how then can I relate my results to my simplify function?

Can I reference them as such:
 
fracOne.simplify(fracOne.add(fracTwo))


Is that legal?
Well you do a loooooooot of mistakes in that code above (shouldn't your compiler be crying ? ^^)

This is not working since simplify doesn't have any arguments you can pass to
fracOne.simplify(fracOne.add(fracTwo))

...and you don't need them

if you just do this:
1
2
3
4
5
6
7
8
void CFrac::add (const CFrac& fraction) //changed int to void and removed const
{
	numerator = numerator * fraction.denominator + fraction.numerator * denominator;

	denominator = denominator * fraction.denominator;

	// return numerator, denominator;  //remove this 
}


what we do now is we change the values inside numerator and denominator. We don't have to return them or anything.

after calling add like this:
1
2
3
4
5
6
fracOne.add(fracTwo); //now the result is in numerator and denominator of fracOne
//if we call now simplify
fracOne.simplify(); // It uses those 2 variables we just changed with "add"
// and does its thing whatever you do there
// QUICK EDIT: since your simplify is private you cant do the above which means:
// you have to call simplify inside of CFrac::add 



Sorry for answering so late I'm kinda busy
Last edited on
I appreciate the feedback, no worries.

Sadly the class functions cannot be changed. They need to be implemented as shown.

I haven't compiled yet since I'm still working on finishing my code. A lot of the above I'm tweaking as I go, so even now its different than what I posted earlier.

Thanks again.
Been working on the code for a bit, its still not ready to run. Hoping I can get some more feedback/direction.

Here are the class parameters I need to stick to, in case its unclear:
CFrac
--------------------------------------------
- numerator : int
- denominator : int
- simplify () : void
--------------------------------------------
+ CFrac (int = 0, int = 1)
+ ~CFrac()
+ add (const CFrac&) const : CFrac
+ subtract (const CFrac&) const : CFrac
+ multiply (const CFrac&) const : CFrac
+ divide (const CFrac&) const : CFrac
+ showFrac () const : void
+ setFrac () : void

I still need to get the calculation functions to work and have the answer simplified for the showFrac output.

Thanks for the feedback.

Current code is looking as such:
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <conio.h>

using namespace std;
//class definition
class CFrac
{
private:
	int numerator, denominator;
	void simplify();

public:
	CFrac (int = 0, int = 1);
	~CFrac();
	int add (const CFrac&) const;
	int subtract (const CFrac&) const;
	int multiply (const CFrac&) const;
	int divide (const CFrac&) const;
	void showFrac () const;
	void setFrac ();
};
//start of class function definition
void CFrac::simplify ()
{
	int num, denom, mod, remainder;

	num = numerator;
	denom = denominator;

	while(mod != 0)
	{
		mod = num / denom;
		remainder = num % denom;
		num = denom;
		denom = remainder;
	}

	numerator /= remainder;
	denominator /= remainder;
}

CFrac::CFrac (int n, int d)
{
	numerator = n;
	denominator = d;
}

CFrac::~CFrac() {}

int CFrac::add (const CFrac& fraction) const
{
	int num, denom;

	num = numerator * fraction.denominator + fraction.numerator * denominator;

	denom = denominator * fraction.denominator;

	return //??
}

int CFrac::subtract (const CFrac& fraction) const
{
	int num, denom;

	num = fraction.numerator * denominator - numerator * fraction.denominator;

	denom = fraction.denominator * denominator;

	return //??
}

int CFrac::multiply (const CFrac& fraction) const
{
	int num, denom;

	num = numerator * fraction.numerator;

	denom = denominator * fraction.denominator;

	return //??
}

int CFrac::divide (const CFrac& fraction) const
{
	int num, denom;

	num = fraction.numerator * denominator;

	denom = fraction.denominator * numerator;

	return //??
}

void CFrac::showFrac () const
{
	int mod, wholeNum;

	mod = numerator % denominator;
	wholeNum = (numerator - mod) / denominator;

	cout << wholeNum << " " << mod << "/" << denominator;
}

void CFrac::setFrac ()
{
	cout << "Enter a fraction: ";
	cin >> numerator;
	cin.ignore(1);
	cin >> denominator;
}
//end of class function definition

int userMenu ();

int main()
{
	CFrac fracOne, fracTwo, fracCalc;

	int firstFraction, secondFraction;

	int menuChoice = 0;

	while(menuChoice != 5)
	{
		menuChoice = userMenu();

		fracOne.setFrac();
		fracTwo.setFrac();

		if(menuChoice == 1)
			fracCalc = fracOne.add(fracTwo);
		else if(menuChoice == 2)
			fracCalc = fracOne.subtract(fracTwo);
		else if(menuChoice == 3)
			fracCalc = fracOne.multiply(fracTwo);
		else if(menuChoice == 4)
			fracCalc = fracOne.divide(fracTwo);
		else
			cout << "Not a valid option, select again." << endl;

		fracCalc.showFrac();
	}

	_getch();
	return 0;
}

int userMenu()
{
	int choice;

	cout << "Fraction Calculation Menu:" << endl
		 << "1 -- ADDITION" << endl
		 << "2 -- SUBTRACTION" << endl
		 << "3 -- MULTIPLICATION" << endl
		 << "4 -- DIVISION" << endl
		 << "5 -- EXIT" << endl
		 << "Enter a choice --> ";

	cin >> choice;

	return choice;
}
Topic archived. No new replies allowed.