-1

Here is my code I have looked up what to do multiple times and still haven't figured out what to do. It keeps giving me this error: C++ expression must be an lvalue or a function designator with the part of the code :

avg_score = (float)*&get_average_score(score_1, score_2, score_3);`

how can i fix the error? the original error was cannot convert a void to a float avg_score = get_average_score(score_1, score_2, score_3); how can i fix the error?`

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
void print_scores(int score_1, int score_2, int score_3);
void get_average_score(int score_1, int score_2, int score_3);
int main()
{
srand(time(NULL));
int score_1, score_2, score_3;
float avg_score;

score_1 = rand() % 21 + 20;
while (score_1 % 2 == 0)
{
    score_1 = rand() % 21 + 20;
}

score_2 = rand() % 21 + 20;
score_3 = rand() % 21 + 20;

print_scores(score_1, score_2, score_3);
avg_score = (float)*&get_average_score(score_1, score_2, score_3);

cout << fixed << setprecision(1);
cout << "Average score = " << avg_score <<
    endl;
return 0;
}

void print_scores(int score_1, int score_2, int score_3)
{
cout << "score 1 = " << score_1 << endl << "score 2 = " << score_2 << endl 
<< "score 3 = " << score_3 << endl;
}

void get_average_score(int score_1, int score_2, int score_3)
{
(float)(score_1 + score_2 + score_3) / (float)3;
}   
  • 1
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Oct 01 '17 at 23:11
  • In short: Please *edit* your question to include the *complete* and *full* error output, copy-pasted as text, including any possible informational notes. Then when you add that, also add a comment in the code, to point out *where* in the code the error(s) are. – Some programmer dude Oct 01 '17 at 23:12
  • `*&` what are you trying to do here? – kmdreko Oct 01 '17 at 23:16
  • 1
    What is `get_average_score` and why doesn't it take a `std::vector`? This is C++. Embrace the Standard Library, don't fight it and make a mess of things. – tadman Oct 01 '17 at 23:16
  • 1
    So you have a function, `get_average_score`, which is declared to *not return a value* (that's what the `void` return type means) and you expect it to return a value? You don't even have a `return` statement in the function (which would give you a compiler error). Perhaps you should [find a good beginners book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read? – Some programmer dude Oct 01 '17 at 23:43
  • Did you just type random things before `get_average_score` in the hope it would start working – M.M Oct 02 '17 at 02:22

1 Answers1

1

Your first mistake lies in the fact that you are trying to get a function that does not return a value to return a value. You've tried to reference a pointer *& which is not how you should be handling this as
1) you've tried to reference a pointer but instead you've done it on a function
2) you want a value, not a pointer so its the wrong approach.

If you need to use pointers (because thats the task at hand) then what you need to do is pass a reference to avg_score into your function.

void get_average_score(float * avg_score, int score_1, int score_2, int score_3)
{
    *avg_score = (score_1 + score_2 + score_3) / 3.0;
}  

and call it in main with:

get_average_score(&avg_score, score_1, score_2, score_3);

and dont forget to update the header declaration:

void get_average_score(float * avg_score, int score_1, int score_2, int score_3);

If you don't have to use pointers the easiest fix is to actually return a value.

Declare the function as type float :

float get_average_score(int score_1, int score_2, int score_3);   

and edit the get_average_score function to be:

float get_average_score(int score_1, int score_2, int score_3)
{
    return (score_1 + score_2 + score_3) / 3.0; 
} 

and get rid of (float)*& from main.

This means your function will return a float value that will be stored in avg_score on return.
Also note, by changing the denominator to 3.0 instead of 3 you don't need to don't need to type cast the result as a float.


Your coding style does come off as a little basic (which is ok, everyone has to start somewhere) but you have room for improvement, so take the time to learn now rather than struggling later (trust me it makes life easy in the long run).
Rather than making a function that will only work when you are averaging 3 numbers why not make a more modular function that would work for as many numbers as you want!

Try learning how to use vectors! If you're coming from C its kinda like an array but can be dynamically allocated i.e. any size you want. Have a look around the net for some tutorials on what vectors are and how to use them. I won't write out the code for this because you should learn how to do it your self (trust me you'll understand it better) but basically using a vector of int's std::vector<int> you can iterate through them all and add each element together and then at the end divide by the total number of elements (the number of iterations you do) to get your average!

**obviously theres a limit but thats a limit of your computer... *

ljden
  • 352
  • 1
  • 11