Ambiguous Error

Doing an exercise from my C++ book and got asked to create a program that takes a double value and using a function rounds it to nearest whole value. I made my program, go the ambiguous error, so checked their code and mine happened to be more or less the same bar a few variable names, and i tried theirs and got the same problem.

Here's the code (line 16):
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
#include <iostream>
#include <cmath>
using namespace std;

void round(double &i);

int main()
{
    double i = 0.0;
    
    cout << "Please enter a decimal number: ";
    cin >> i;
    cin.ignore();
    
    cout << "\n\n'i' rounded to the nearest value is ";
    round(i);
    
    cout << i;
    
    cin.get();
    return 0;
}

void round(double &i)
{
     double fractpart = 0.0; intpart = 0.0;
     
     fractpart = modf(i, &intpart)
     
     if(fractpart<0.5) i = intpart;
     else intpart+=1.0;
}


I don't understand why though because there's only one function so its not like i'm overloading incorrectly. And 'i' is double like the function requires.

The error message says:
9 C:\Users\alan\Desktop\trial.cpp call of overloaded `round(double&)' is ambiguous
note C:\Dev-Cpp\include\math.h:672 candidates are: double round(double)
note C:\Dev-Cpp\include\math.h:672 void round(double&)
Well, before getting to these errors, you have a few other issues to fix.

1
2
3
4
5
6
7
8
9
void round(double &i)
{
     double fractpart = 0.0; intpart = 0.0;
     
     fractpart = modf(i, &intpart)
     
     if(fractpart<0.5) i = intpart;
     else intpart+=1.0;
}


1. intpart has no type declared. You need to put int intpart = 0.0;
2. You're missing a semicolon at the end of this line fractpart = modf(i, &intpart)
My bad, just typo's . Fixed.
closed account (z05DSL3A)
9 C:\Users\alan\Desktop\trial.cpp call of overloaded `round(double&)' is ambiguous
note C:\Dev-Cpp\include\math.h:672 candidates are: double round(double)
note C:\Dev-Cpp\include\math.h:672 void round(double&)

Your cmath(math.h) has already got a round() function defined and can not decide on the appropriate version.
Thank you Grey Wolf!

Thats quite poor tbh though with the book because thats the second time this has happened. The last time was with the variable 'count' - apparently something already in the std namespace that is called 'count. I was under the impression Herbert Schildt was one of the top programmers.

Edit: Then again maybe i'm being too harsh on possibly 'simple' mistakes. Afterall, the book is actually vary good and for such a vast amount of examples and explanations, errors are likely to occur.
Last edited on
closed account (z05DSL3A)
Well, as far as I know, round() is not a standard part of cmath(math.h); so it looks like an oddity Dev C++.
Last edited on
To round to a whole number,
simply add .5 to the number then cast it to an int.
1
2
3
double number = 3.6;
int round = static_cast<int>(number+.5);
cout<<"whole number = "<<round<<endl;
You can always use the floor and ceil functions for rounding up and down as well.
@ Return 0;

ya but note that these functions don't round the number,so ceil(.4) would return 1 and floor(.6) would return 0.
You could right a function to make these work, but i find it easier just to cast the number. Either way is fine though
How does ceil and floor not round the numbers up or down?
closed account (z05DSL3A)
Adding 0.5 and casting is not a good solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int round(double number)
{
    return static_cast<int>(number + 0.5);
}

int main()
{
    double a(-3.4);
    double b(-3.6);
    std::cout << a << " rounds to " << round(a) << std::endl;
    std::cout << b << " rounds to " << round(b) << std::endl;

    return 0;
}


-3.4 rounds to -2
-3.6 rounds to -3


Edit:
1
2
3
4
int round(double number)
{
    return static_cast<int>(number > 0.0 ? number + 0.5 : number - 0.5);
}

Last edited on
I meant they dont round up if not greater then 1, so ceil(.4) = 1. But Grey wolf , some how i forgot about the existance of negitive numbers lol
here :
1
2
3
4
5
6
7
8
9
double num, x = .5;
int round;
cin>>num;
if(num<0)
   x = -.5;

round = static_cast<int>(num + x);
cout<<round<<endl;


seem easier then writing conditions for ceil and floor.. my opinion
Yeah you're right.
Topic archived. No new replies allowed.