0

Below is the code that I'm having trouble with. There is an error in the line avg += abs(num) but I can't solve it. Error code:

error: call of overloaded 'abs(double&)' is ambigous

I realize that it is pretty basic but in the book I am using "C++: A Beginner's Guide (second edition)" by Herbert Schildt the code is identical in one of the examples used here:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    double num, avg;
    int x = 5;
    int i;

    avg = 0.0;

    for (i = 1; i <= x; ++i){

        cout << "Enter value " << i << ": ";
        cin >> num;

        avg += abs(num);

    }

    avg /= x;

    cout<< endl;
    cout << "The average is " << avg;
    return 0;
}
  • 5
    Where is your question? And what is it? – Maroun Jul 29 '13 at 11:29
  • I am getting an error when I compile the code. However, I can't find a solution to the error. The line where the error is occurring is "avg += abs(num);" –  Jul 29 '13 at 11:31
  • 1
    What are you compiling with, and how? Your code compiles and works for me. – DUman Jul 29 '13 at 11:32
  • probably you need to include and link with -lm – hetepeperfan Jul 29 '13 at 11:32
  • 1
    @user2583389 You are posting in the comments, where posting inside your question is more approriate as already mentionned. – hetepeperfan Jul 29 '13 at 11:35
  • Get a different book: http://stackoverflow.com/questions/2054939/char-is-signed-or-unsigned-by-default (2nd comment) or http://en.wikipedia.org/wiki/Herbert_Schildt#Reception – doctorlove Jul 29 '13 at 11:36
  • 4
    Upon closer inspection, the code sucks. It declares `avg` as an `int` and then tries to use it as a `double`. And includes `cstdlib` where `cmath` would suffice. And starts a loop at 1 instead of 0 for no good reason. If that code is from a book, you need a different book. – DUman Jul 29 '13 at 11:40
  • avg was set to int by me to try and solve the problem but I forgot to change it back sorry. It starts the loop at 1 because it uses i's value to tell the user which value he is entering e.g. value 1, value 2. Lastly, i assume he used cstdlib because he was waiting till later to introduce cmath in the book. –  Jul 29 '13 at 11:45

2 Answers2

4

You are not including the correct header for the floating-point overloads of std::abs:

#include <cmath>
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • The book never included this. Does it imply that the book may be wrong? –  Jul 29 '13 at 11:50
  • @user2583389: Yes; if the book doesn't include this, then it's wrong. (Note that the author has a bit of a reputation for writing poor books, so I wouldn't be surprised if this one isn't particularly good. You might find [these recommendations](http://stackoverflow.com/questions/388242) useful instead.) – Mike Seymour Jul 29 '13 at 11:52
  • It is possible that on some variant of C++ library, the `#include ` or `#include ` just so happens to also drag in the `` header - it is not unknown for this to be the case. But using `abs`, should make YOUR code want to add `#include ` in your code - always include everything YOU need - whether it happens to compile without it or not. – Mats Petersson Jul 29 '13 at 11:58
3

The code of this book is :

#include <iostream>
#include <cmath>
//        ^^^^^

using namespace std;

int main()
{
    double num, avg; // The type is double not int !!!
  //^^^^^^
    int i;

    avg = 0.0;

    for (i = 0; i < 5; ++i){
       //    ^      ^

        cout << "Enter value : ";
        //                   ^^^^
        cin >> num;

        avg += abs(num);
    }

    avg /= 5;
    //     ^

    cout<< endl;
    cout << "The average is " << avg;
    return 0;
}

I found it on a copy of this book...

And for std::abs you should include the header cmath :

#include <cmath>

Documentation : http://www.cplusplus.com/reference/cmath/abs/

EDIT : But the function abs declared in cstdlib has as prototype :

          int abs (          int n);
     long int abs (     long int n);
long long int abs (long long int n);

But in cmath :

     double abs (double x);
      float abs (float x);
long double abs (long double x);
     double abs (T x);

But here it seems, you want to work with double (if not, you will lose the decimal part of this : avg /= 5). So the cmath version fit better. Otherwise, your code should be :

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int num, avg;
  //^^^^^^
    int i;

    avg = 0;
    //    ^

    for (i = 0; i < 5; ++i){
        cout << "Enter value : ";
        cin >> num;
        avg += abs(num);
    }

    avg /= 5; // but you loose the decimal part...

    cout<< endl;
    cout << "The average is " << avg;
    return 0;
}
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62