Fractions

closed account (4wpL6Up4)
Hi,

I am writing a program that lets the user input two fractions and choose which arithmetical operation to use.
I am not sure why, but the program does not run and I get error 2601 for void(multiply) and void(divide).
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
164
165
166
167
168
  #include "pch.h"
#include <stdlib.h>
#include<iostream>
#include<cstdlib>
#include<cctype>
#include <sstream>
#include <string>
#include<cmath>
#include <algorithm>
#include <iterator>
#include <ctype.h>
#include <iomanip>
#include <limits>
#include<cstdlib>


using namespace std;

void getnumbers(int &x, int  &y, int &z, int &w);
void add(int &x, int  &y, int &z, int &w);
void subtract(int &x, int  &y, int &z, int &w);
void multiply(int &x, int  &y, int &z, int &w);
void divide(int &x, int  &y, int &z, int &w);

int main()
{
	int x,y,z,w;
	char answer, operation;
	do { 
		do {
			getnumbers(x, y, z, w);
			cout << "add,subtract,multiply,divide(a/s/m/d)?" << endl;
			cin >> operation;
		} while ((operation != 'a') && (operation != 's') && (operation != 'm') && (operation != 'd'));
		switch (operation)
		{
		case 'a':
			add(x, y, z, w);
			break;
		case 's':
			subtract(x, y, z, w);
			break;
		case 'm':
			multiply(x, y, z, w);
			break;
		case 'd':
			divide(x, y, z, w);
			break;
		default:
			exit(0);
		}
		cout << "try again(Y/N)?";
		cin >> answer;
	} while (answer == 'Y' || answer == 'y');
	return 0;
}

void getnumbers(int &x, int  &y, int &z, int &w)
{
	cout << "enter num and den first fraction" << endl;
	cin >> x;
	cin >> y;
	cout << x << "/" << y << endl;
	cout << "enter num and den second fraction" << endl;
	cin >> z;
	cin >> w;
	cout << z << "/" << w << endl;
}

void add(int &x, int  &y, int &z, int &w)
{
	int numres = (x*w + y * z);
	int denres = (y*w);
	

	int gcd1 = (numres % denres);
	int gcd2 = (denres%numres);

	

	if (numres > denres)
	{
		cout <<x<<"/"<<y<<"+"<<z<<"/"<<w<<"="<< numres / gcd1 << "/" << denres/gcd1  << endl;

	}
	else if (denres > numres)
	{
		cout << x << "/" << y << "+" << z << "/" << w << "=" << numres / gcd2 << "/" << denres / gcd2 << endl;
	}
	else if (denres = numres)
	{
		cout << x << "/" << y << "+" << z << "/" << w << "=" << "1" << endl;
	}
}

void subtract(int &x, int  &y, int &z, int &w)
{
	int numres = (x*w - y * z);
	int denres = (y*w);


	int gcd1 = (numres % denres);
	int gcd2 = (denres%numres);

	if (numres > denres)
	{
		cout << x << "/" << y << "-" << z << "/" << w << "=" << numres / gcd1 << "/" << denres / gcd1 << endl;

	}
	else if (denres > numres)
	{
		cout << x << "/" << y << "-" << z << "/" << w << "=" << numres / gcd2 << "/" << denres / gcd2 << endl;
	}
	else if (denres = numres)
	{
		cout << x << "/" << y << "-" << z << "/" << w << "=" << "1" << endl;
	}


	void multiply(int &x, int  &y, int &z, int &w)
	{
		int numres = (x*z);
		int denres = (y*w);


		int gcd1 = (numres % denres);
		int gcd2 = (denres%numres);

		if (numres > denres)
		{
			cout << x << "/" << y << "*" << z << "/" << w << "=" << numres / gcd1 << "/" << denres / gcd1 << endl;

		}
		else if (denres > numres)
		{
			cout << x << "/" << y << "*" << z << "/" << w << "=" << numres / gcd2 << "/" << denres / gcd2 << endl;
		}
		else if (denres = numres)
		{
			cout << x << "/" << y << "*" << z << "/" << w << "=" << "1" << endl;
		}
	}

	void divide(int &x, int  &y, int &z, int &w)
	{
		int numres = (x*w);
		int denres = (y*z);


		int gcd1 = (numres % denres);
		int gcd2 = (denres%numres);

		if (numres > denres)
		{
			cout << x << "/" << y << "*" << z << "/" << w << "=" << numres / gcd1 << "/" << denres / gcd1 << endl;

		}
		else if (denres > numres)
		{
			cout << x << "/" << y << "*" << z << "/" << w << "=" << numres / gcd2 << "/" << denres / gcd2 << endl;
		}
		else if (denres = numres)
		{
			cout << x << "/" << y << "*" << z << "/" << w << "=" << "1" << endl;
		}
	}

}
You define the functions multiply() and divide() inside the scope of subtract(). That is not allowed. Move that functions outside subtract(). Beyound the } on line 168.
There's a brace or two missing, and you also have assignment operators (=) rather than equality tests (==) in lines 90, 114, 138, and 162. (i.e. line numbers in your code above, not the code below).

Also seems like a number of headers that you don't really need. I *think* you'll find the code below works as you expect it to. (although I haven't really tested whether the results you provide to the user are actually correct).

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
#include<iostream>
#include <algorithm>  // you also don't seem to be using this as yet
#include <iomanip>    // you also don't seem to be using this either

using namespace std;

void getnumbers(int &x, int  &y, int &z, int &w);
void add(int &x, int  &y, int &z, int &w);
void subtract(int &x, int  &y, int &z, int &w);
void multiply(int &x, int  &y, int &z, int &w);
void divide(int &x, int  &y, int &z, int &w);

int main()
{
   int x,y,z,w;
   char answer, operation;
   do { 
	do {
	   getnumbers(x, y, z, w);
	   cout << "add,subtract,multiply,divide(a/s/m/d)?" << endl;
	   cin >> operation;
	} while ((operation !='a')&& (operation !='s')&& (operation !='m')&& (operation !='d'));

        switch (operation)
        {
	case 'a':
		add(x, y, z, w);
		break;
	case 's':
		subtract(x, y, z, w);
		break;
	case 'm':
		multiply(x, y, z, w);
		break;
	case 'd':
		divide(x, y, z, w);
		break;
	default:
		exit(0);
	}

	cout << "try again(Y/N)?";
	cin >> answer;

   } while (toupper(answer) == 'Y');

return 0;
}

void getnumbers(int &x, int  &y, int &z, int &w)
{
	cout << "enter num and den first fraction" << endl;
	cin >> x;
	cin >> y;
	cout << x << "/" << y << endl;
	cout << "enter num and den second fraction" << endl;
	cin >> z;
	cin >> w;
	cout << z << "/" << w << endl;
}

void add(int &x, int  &y, int &z, int &w)
{
	int numres = (x*w + y * z);
	int denres = (y*w);
	int gcd1 = (numres % denres);
	int gcd2 = (denres%numres);

	if (numres > denres)
	{
	cout <<x<<"/"<<y<<"+"<<z<<"/"<<w<<"="<< numres / gcd1 
             << "/" << denres/gcd1  << endl;
	}
	else if (denres > numres)
	{
	cout << x << "/" << y << "+" << z << "/" << w << "=" << numres / gcd2 
             << "/" << denres / gcd2 << endl;
	}
	else if (denres == numres)
	{
	cout << x << "/" << y << "+" << z << "/" << w << "=" << "1" << endl;
	}
}

void subtract(int &x, int  &y, int &z, int &w)
{
	int numres = (x*w - y * z);
	int denres = (y*w);
	int gcd1 = (numres % denres);
	int gcd2 = (denres%numres);

	if (numres > denres)
	{
	cout << x << "/" << y << "-" << z << "/" << w << "=" << numres / gcd1 
             << "/" << denres / gcd1 << endl;
	}
	else if (denres > numres)
	{
	cout << x << "/" << y << "-" << z << "/" << w << "=" << numres / gcd2 
             << "/" << denres / gcd2 << endl;
	}
	else if (denres == numres)
	{
	cout << x << "/" << y << "-" << z << "/" << w << "=" << "1" << endl;
	}
}

void multiply(int &x, int  &y, int &z, int &w)
{
	int numres = (x*z);
	int denres = (y*w);
	int gcd1 = (numres % denres);
	int gcd2 = (denres%numres);

	if (numres > denres)
	{
	cout << x << "/" << y << "*" << z << "/" << w << "=" << numres / gcd1 
             << "/" << denres / gcd1 << endl;
	}
	else if (denres > numres)
	{
	cout << x << "/" << y << "*" << z << "/" << w << "=" << numres / gcd2 
             << "/" << denres / gcd2 << endl;
	}
	else if (denres == numres)
	{
	cout << x << "/" << y << "*" << z << "/" << w << "=" << "1" << endl;
	}
}

void divide(int &x, int  &y, int &z, int &w)
{
	int numres = (x*w);
	int denres = (y*z);
	int gcd1 = (numres % denres);
	int gcd2 = (denres%numres);

	if (numres > denres)
	{
	cout << x << "/" << y << "*" << z << "/" << w << "=" << numres / gcd1 
             << "/" << denres / gcd1 << endl;
	}
	else if (denres > numres)
	{
	cout << x << "/" << y << "*" << z << "/" << w << "=" << numres / gcd2 
             << "/" << denres / gcd2 << endl;
	}
	else if (denres == numres)
	{
	cout << x << "/" << y << "*" << z << "/" << w << "=" << "1" << endl;
	}
}


Hope that helps.
Last edited on
Topic archived. No new replies allowed.