My average function for c++ keeps getting errors

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 6811 Views - Last Post: 30 June 2012 - 02:15 PM Rate Topic: -----

#1 snesnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-June 12

My average function for c++ keeps getting errors

Posted 24 June 2012 - 01:34 PM

#include <iostream>
#include <cmath> // This is simply a library of math functions we can use.
using namespace std;

void Average(int value1, int value2, int value3, int value4)

{
    ((value1 + value2 + value3 + value4)/4);
}

int main()

{
    int value1,value2,value3,value4;
    double average;
    cout << "We are going to take the average of four values. Please type 4 numbers you would like to take the average of.\n";
    cin >> value1 >> value2 >> value3 >> value4;
    cout << "The average is " << Average(value1, value2, value3, value4) << endl;
}



Im not sure how to put tags around my code for this site, but I tried. Anyway I just want to create a function for averaging 4 numbers. In other words, I am relying on #include <cmath> so that I can use the functions. Problem is when I run this program, theirs errors, and I am not sure why. What is wrong with this code?

This post has been edited by Oler1s: 24 June 2012 - 02:00 PM
Reason for edit:: It's /code, not \code


Is This A Good Question/Topic? 0
  • +

Replies To: My average function for c++ keeps getting errors

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: My average function for c++ keeps getting errors

Posted 24 June 2012 - 02:01 PM

Quote

Problem is when I run this program, theirs errors, and I am not sure why. What is wrong with this code?
What errors? Please provide all information. If you have errors, copy paste or describe your observations verbatim.
Was This Post Helpful? 0
  • +
  • -

#3 snesnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-June 12

Re: My average function for c++ keeps getting errors

Posted 24 June 2012 - 02:13 PM

error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<...
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9206
  • View blog
  • Posts: 34,588
  • Joined: 12-June 08

Re: My average function for c++ keeps getting errors

Posted 24 June 2012 - 02:16 PM

... well does "Average" actually return a value? Void's tend to not.
Was This Post Helpful? 1
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: My average function for c++ keeps getting errors

Posted 24 June 2012 - 02:18 PM

Don't chop off errors please. Post the entire thing.

Anyway, that's not necessary here because I recognized the issue from the beginning of the error message.

Here's your average function: void Average(int value1, int value2, int value3, int value4)

So your average functions takes in 4 values. And out comes nothing.

How do you get the average from your average function??
Was This Post Helpful? 0
  • +
  • -

#6 snesnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-June 12

Re: My average function for c++ keeps getting errors

Posted 24 June 2012 - 02:48 PM

I was going to put it in double rather then void, but for some reason I was told to put it in void. Dont ask me why. I was just following directions lol. In any case, I understand average. Its just that I have to use a function for average from <cmath>. Instead of void, I probably would of used double.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: My average function for c++ keeps getting errors

Posted 24 June 2012 - 02:54 PM

I don't understand what you mean by:

Quote

Its just that I have to use a function for average from <cmath>

There is no average function in cmath. In fact you're not using anything that requires the cmath header to be included.

Also you may want to study the excellent function tutorials provided in my signature.

Jim

This post has been edited by jimblumberg: 24 June 2012 - 02:56 PM

Was This Post Helpful? 1
  • +
  • -

#8 Lionheart Richard King  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-June 12

Re: My average function for c++ keeps getting errors

Posted 25 June 2012 - 12:33 PM

The reason why people arent very helpful is because you need to explain what you trying to do with the program. Example-

do you want to call a function from main to display output?

do you want main itself to display the output? what do you want?

if your average function is declared as void, you need to have the function do the output. otherwise, it has to return SOMETHING to the call point in main. review your notes or chapter on functions. you dont even need to include any extras libraries for this, either.
Was This Post Helpful? 0
  • +
  • -

#9 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 08:17 AM

Your function Average(int, int, int, int) is not returning a value (because it is a void function). So the reason you are getting that error is because the << operator from your cout looks for a return value from a function call. Since there is no return value (void function), it throws this error.

Your best bet if you HAVE to use a void function for Average() is to do something like this:

#include <iostream>

using namespace std;

void Average(int value1, int value2, int value3, int value4)

{
    cout << ((value1 + value2 + value3 + value4)/4) << endl;
}

int main()

{
    int value1,value2,value3,value4;
    /*double average; NOTE: You don't need this because your are not 
      returning a value from Average(int, int, int, int)*/
    cout << "We are going to take the average of four values. Please type 
             4 numbers you would like to take the average of.\n";
    cin >> value1 >> value2 >> value3 >> value4;
    cout << "The average is ";
    Average(value1, value2, value3, value4);
    return 0;
}




Since your void Average() function doesn't return a value, the compiler sees something more like this:
cout << "The average is " <<;


which is obviously unacceptable since the operator<< function requires two arguments.

PS. The reason I removed the #include <cmath> is because you don't need it.

This post has been edited by axnjxn: 27 June 2012 - 08:32 AM

Was This Post Helpful? 1
  • +
  • -

#10 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 08:58 AM

In addition, your function Average(int, int, int, int) is flawed in that it performs math functions on the parameters, but it doesn't go anywhere. In other words, it doesn't get passed to cout via <<, it doesn't get assigned to a variable via =, it doesn't get returned (void function), and it doesn't get passed to another function. This:

void Average (int value1, int value2, int value3, int value4) {

    double average = ((value1 + value2 + value3 + value4)/4);
}


at least makes sense since the ((value1 + value2 + value3 + value4)/4) portion actually goes somewhere. But even then it is of no use because average isn't printed, returned, or passed. Honestly, if this is for a class, I'm not sure I understand the purpose of having you use a VOID function for Average() instead of having it set to double and return the average to be used in the main() function.
Was This Post Helpful? 1
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,332
  • Joined: 08-August 08

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 09:04 AM

You could also pass by reference, although I wouldn't do it in this case:
#include <iostream>

using namespace std;

void Average(int value1, int value2, int value3, int value4, double &theaverage)

{
    theaverage = ((value1 + value2 + value3 + value4)/4.0); // int/double will return double, but int/int would return int so 3/4 would return 0
}

int main()

{
    int value1,value2,value3,value4;
    double average;
    cout << "We are going to take the average of four values. Please type 
             4 numbers you would like to take the average of.\n";
    cin >> value1 >> value2 >> value3 >> value4;
    Average(value1, value2, value3, value4, average);
    cout << "The average is " << average << endl;
    return 0;
}


Was This Post Helpful? 1
  • +
  • -

#12 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 09:08 AM

Very true. But at the end of the day, this should be directly in the main() function anyway since a lot of overhead is involved in a simple single use function like this. The whole purpose, I'm pretty sure, is a class exercise to learn about calling functions and passing parameters. Pass by reference being a bit beyond the scope of the current lesson. I could be wrong, but judging from the OP, I would say I'm pretty spot on ;)

This post has been edited by axnjxn: 27 June 2012 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#13 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2114
  • View blog
  • Posts: 3,240
  • Joined: 21-June 11

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 09:16 AM

View Postaxnjxn, on 27 June 2012 - 06:08 PM, said:

But at the end of the day, this should be directly in the main() function anyway since a lot of overhead is involved in a simple single use function like this.


There's nothing wrong with single-use functions - in fact they're a perfectly nice way to increase the readability of your code.

Even if for some reason the function call would not get inlined, a single function call would hardly be "a lot of overhead". Unless you were referring to notational overhead (writing down the method signature), but that's pretty tame as well.
Was This Post Helpful? 0
  • +
  • -

#14 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 09:33 AM

First, a question: Don't you have to explicitly define a function as being INLINE unless it is declared and defined within the confines of a class definition?

But in this case, in comparison to the total activity of the program, I'd say the overhead is pretty steep. So far, we're copying 4 variables in via pass-by-value parameters and if the function isn't inline, we also have function call overhead. Since the program itself only declares 4 variables, I'd say it would be much more efficient to simply put the average calculation right in the cout statement. Of course there's nothing wrong with a single call function to organize code, but in this case I'd say your stretching it.

This post has been edited by axnjxn: 27 June 2012 - 09:37 AM

Was This Post Helpful? -1
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: My average function for c++ keeps getting errors

Posted 27 June 2012 - 09:52 AM

Quote

But in this case, in comparison to the total activity of the program, I'd say the overhead is pretty steep.

Since this program is about learning C/C++ constructs, why are you so worried about function call overhead? It is much easier to learn how to implement and call functions when the function it's self is trivial. So in my opinion this is an excellent way of learning function call semantics.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2