'sin': redefinition; different exception specifications

Hello all, I am doing a slightly flexible calculator, I want to add the sin and cosine functions. Although I am certain the sin function works, I get the error message 'sin': redefinition; different exception specifications.

The code is down below.

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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  #include <iostream>
#include <cmath>
#include <cstdlib>
#include<iomanip>
#include <ctime>


using namespace std;

void menu();
float add(float&);
float subtract(float&);
float divide(float&, float&);
float multiply(float&);
float sqcube(float&);
float sin(float);
int fact(int);






int main() {



	int choice;
	cout << "================================================================================================\n"
		<< "======================================================================================\n \n \n \n \n";
	menu();
	cout << "Pick an operation from the menu: "; cin >> choice;

	float x, y, n, sum, tsum, vsum, pro, div, result;
	char cont, sq;
	sum = 0, tsum = 0, vsum = 1;


	switch (choice)
	{
	case 0: {
		add(sum);
		cout << sum << " ";
		break;
	}
	case 1: {
		subtract(tsum);
		cout << tsum << " ";
		break;
	}
	case 2: {
		divide(x, y);
		div = divide(x, y);
		cout << div << " ";
		break;
	}
	case 3: {
		sqcube(result);
		cout << result << " ";
		break;
	}

	}

}

















void menu() {
	using namespace std;

	cout << "    -------------Basic Calculator---------                \n";
	cout << "  Choose a number on the menu    \n";
	cout << "0. Add \n";
	cout << "1.Subtract\n";
	cout << "2.Divide\n";
	cout << "3.Multiply\n";
	cout << "4.Modulus\n";
	cout << "5.Square or cube\n";
	cout << "6.Any power\n";
	cout << "7.Square root of a number\n";
	cout << "8. Sin\n";
	cout << "9. Cos\n";
	cout << "10. Exit\n";
}

float add(float& sum) {
	int n, x;
	sum = 0;
	cout << "How many numbers do you want to add?: "; cin >> n;
	cout << "Enter " << n << " numbers\n";
	for (int i = 1; i <= n; ++i) {
		cin >> x;
		sum += x;
	}
	return sum;
}


float subtract(float& sum) {
	int n, x;
	sum = 0;
	cout << "How many numbers do you want to subtract?: "; cin >> n;
	cout << "Enter " << n << " numbers\n";
	for (int i = 1; i <= n; ++i) {
		cin >> x;
		sum -= x;
	}
	return sum;
}


float multiply(float& product) {
	int n, x;
	product = 1;
	cout << "How many numbers do you want to multiply?: "; cin >> n;
	cout << "Enter " << n << " numbers\n";
	for (int i = 1; i <= n; ++i) {
		cin >> x;
		product *= x;
	}
	return product;
}

float divide(float& a, float& b) {
	int choose;
	cout << "Enter two numbers of your choice \n";
	cout << "Number 1: "; cin >> a;
	cout << "Number 2: "; cin >> b;
	cout << "Options:\n"
		<< "1. divide num 1/num2"
		<< "2. divide num 2/num1"; cin >> choose;
	if (choose == 1) {
		return (a / b);
	}
	else if (choose == 2) {
		return (b / a);
	}
	else {
		cout << "You can only choose 1 or 2. Enter again: "; cin >> choose;
	}
}

float sqcube(float& output) {
	float x;
	char sc;
	cout << "Enter your number to square or cube: "; cin >> x;
	cout << "Do you want to square or cube? type 's' for square, 'c' for cube: "; cin >> sc;
	if (sc == 's') {
		output = x * x;
	}
	else if (sc == 'c') {
		output = x * x * x;
	}
	else {
		cout << "Invalid character\n";
	}
	return output;
}
float sin(float x) {

	int i, j, n, fact, sign = -1;
	float p, sum = 0;
	cout << "Enter the value of x : ";
	cin >> x;
	cout << "Enter the value of n : ";
	cin >> n;

	for (i = 1;i <= n;i += 2)
	{
		p = 1;
		fact = 1;
		for (j = 1;j <= i;j++)
		{
			p = p * x;
			fact = fact * j;
		}
		sign = -1 * sign;
		sum += sign * p / fact;
	}
	cout << "sin " << x << "=" << sum;
}
int fact(int n) {
	int x, output = 1;
	for (int i = 1; i < x; ++i) {
		output *= i;
	}
	return output;
}
Last edited on
sin is a standard math function.

Pick a different name.
Alright I changed it to sine and added the return statement at the end, but I still get an error uninitialized local variable 'x' used
Line 198: int x, output = 1; what is the value of x?

You have n as your function parameter, you never use it. Maybe if you use n as your for loop test condition instead of x?
Oh yes true! I removed x. But now when I run the code, I still get an error saying, x is uninitialized, even though I have a cin statement
I added case 4 in the loop

so it was something like this
1
2
3
4
5
case 4: {
sin(x);
cout<<"the value of sin is "<<sum<<"\n";
break;
}

it will ask the user for n but will not ask for x and will calculate anyway
Last edited on
You have other problems lurking in the shadows waiting to pounce.

Your sine function doesn't return a value, your divide function has control paths that don't return a value.

You are passing reference(s) into several of your functions and then return a value that gets copied.

You declare 4 variables at lines 34-35 you never use. cont, n, pro and sq.

Why all the obscure variable names? variable names should document what they are used for.

YOU know now what they mean now. We don't, and later neither will you.
I still get an error saying, x is uninitialized, even though I have a cin statement

And the line where this mysterious error happens?

That code snippet you posted will not show the calculated sine because you are not saving the value the function returns. Neither are you passing a referenced variable into the function.
Speaking of my divide function, I don't really understand, it does return a value but the issue is that asks to enter numbers twice. I am not sure how to fix that. My sine value now returns a value but I am not sure where I am going wrong
There is no return statement at the end of the divide function. Your if statements might return before the function ends, they may not.

What happens when you enter a choice other than 1 or 2? The function ends without a return.

Your uninitialized variable error is a scoping problem. The x in main is NOT the same x variable in your sine function.

Your x in sine is a COPY of the x in main. A local copy. It doesn't matter what you do with it, x is NOT changed in main.
Last edited on
Topic archived. No new replies allowed.