#include <iostream>
usingnamespace std;
int max(int a, int b)
{
if (a > b)
return a;
elsereturn b;
}
int add(int a, int b)
{
return (a+b);
}
int main()
{
cout << add(2.5,3.5) << endl; // this returns 5, correct
cout << max(2.5, 3.5) << endl; // this should returns 3, but 3.5, why?
return 0;
}
My Question is on the following both line:
1 2
cout << add(2.5,3.5) << endl; // this returns 5, correct
cout << max(2.5, 3.5) << endl; // this should returns 3, but it returns 3.5, why?
I really don't know how to explain this in more detail. Why do you think your code should return 3, when the two numbers that it has to deal with are 2.5 and 3.5? Do you understand what is going on in max() and add()?
Huh...
I agree with OP. I should return 3 with the implicit type conversion. I don't know why it doesn't.
EDIT: Oh. I think I found it. It must be conflicting with the existing double max(double,double) function which iostream includes. The compiler automatically chooses the most appropriate function and calls that.
Try renaming your function to anything else and it should work.