Can't find the error? (short code)

This is a simple code that demonstrates the use of a user made "round" function, but i'm running into a problem with the type cast in this program. I've written a program that avoids the type cast, but it's driving me crazy not being able to figure out exactly why this particular program isn't running.

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
#include <iostream>
#include <cmath>
using namespace std;

int round(double number);

int main()
{
	double doubleValue;
	char ans;
	
	do
	{
		cout << "Enter a double value: ";
		cin >> doubleValue;
		cout << "Rounded that number is " << round(doubleValue) <<
			endl;
		cout << "Again? (y/n): ";
		cin >> ans;
	} while (ans == 'y' || ans == 'Y');
	cout << "End of testing.\n";

	return 0;
}

	int round(double number)
	{
		return static_cast<int>(floor(number + 0.5));
	}
This topic is a continuation of http://www.cplusplus.com/forum/beginner/162165/

The problem sems to actually be a compiler bug - there is a round function defined in <cmath> and your function is being considered an overload of it. Give your function a different name or put it in its own namespace to work around the compiler bug.
The function round(...) exists in the namespace std. Use another name like myround(...) or so.

By the way: It works for positive values only.
I knew it was a bug of some sort! I didn't want to suggest that it was and have it be something simple.

Thanks so much! Changing the name worked. I'll try to keep it confined to one post.
Last edited on
Why was this code written with a type cast? Would this program be any different if "round" was declared as type double?
The problem sems to actually be a compiler bug


Is it really considered a compiler bug? I mean, isn't one of the reasons of using std:: instead of using namespace std this specific reason, so it doesnt become ambiguous ?
Last edited on
@SoloPopo: Because whole numbers should be stored in integers, not floating point types.

@TarikNeaj: if you delete using namespace std; the error still occurs. This would make sense for <math.h> but not for <cmath>.
http://ideone.com/QIG8vl
http://coliru.stacked-crooked.com/a/ac98e0845d535c15
The fact that is happens for two compilers in two standard library implementations makes me doubt it is a bug, but it doesn't make sense for it not to be.
Last edited on
Topic archived. No new replies allowed.