Here is my code, it is suppose to take the double num, round it, and return it using call by reference, but when i try to compile it gives me an ambiguity error at round(i); .

please help.

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

void round(double &num);
int main()
{
    string line;
    
    double i = 100.7;
    cout << "Number i before rounding: " << i << "\n";
    round(i);
    cout << "Number i after rounding: " << i << "\n";
    
    double j = -3.3;
    cout << "Number j before rounding: " << j << "\n";
    round(j);
    cout << "Number j after rounding: " << j << "\n";
    
    getline(cin,line);
    cin.get();
    return 0;
}

void round(double &num)
{
     int value;
     int fraction;
     
     fraction = modf(num, &value);
     
     if(fraction < 0.5) num = value;
     else num = value + 1;
}

Recommended Answers

All 2 Replies

line 30: modf() takes pointers to two doubles. value is not a double. And it returns a double, not an integer. Please read the documentation for functions you use.

Thanks, I was able to solve it by changing the function to void round(double *num) and the call to round(&i)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.