I'm using Herb Schildt's C++ Beginners Guide, using this program from Module 1 Mastery:
// Average the absolute values of 5 numbers.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i;
double avg, val;
avg = 0.0;
for(i=0; i<5; i++) {
cout << "Enter a value: ";
cin >> val; avg = avg + abs(val);
}
avg = avg / 5;
cout << "Average of absolute values: " << avg;
return 0;
}
The line in bold is giving me an error in the Dev++ compiler:
In function `int main()':
call of overloaded `abs(double&)' is ambiguous
candidates are: int abs(int)
long long int __gnu_cxx::abs(long long int)
long int std::abs(long int)
The program above is copied directly from the book - is there an error in the program?
You are trying to give it a double. That double can be cast into an int, or a long int. Both options are viable, so it is ambiguous; which one do you mean? Do you want to cast it to an int, or a long int? Yes, there is an error in the program.
Blimey, well I never. To be honest, Moschops, I'm only about 60 pages into this book and I've spotted several errors, or what I think are errors.
For example, he has a table that shows this:
int -2,147,483,648 to 2,147,483,647
unsigned int -2,147,483,648 to 2,147,483,647
signed int 0 to 4,294,967,295
short int -32,768 to 32,767
unsigned short int -32,768 to 32,767
signed short int 0 to 65,535
and I'm sure the figures for unsigned int and signed int, and unsigned short int and signed short int are the wrong way around.
So Dev-C++ hasn't been updated in 6 yrs - perhaps I'll look at one of the alternatives.
Thanks for your response Moschops, much appreciated.