Help me! Why it doesn't compile?

#include <iostream>
#include <cmath>
using namespace std;

int round(double number);
// Assumes number >=0.

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." << endl;

}

int round(double number)
{
return static_cast<int> (floor(number+0.5));
}
It compiles fine for me when I test it, what kind of errors are you getting?
My gcc compilers says there's sth wrong with the line

"int round(double number)"

and

"
int round(double number)
{
return static_cast<int> (floor(number+0.5));
}
"
Because you have syntax errors</troll>

A function called round exists in cmath, causing your function to conflict with it. You don't need to call floor when rounding the number, casting to an int implicitly rounds down. Just remove the call to floor and don't include cmath.
When I try to compile with gcc it doesn't compile because round clash with the round function in math.h that unfortunately is included from the cmath header. I see no other way than to rename your round function or put it inside a namespace.
Thanks everyone. It works.
Topic archived. No new replies allowed.