Void Function problem

Trying to use two void functions to display average of three test scores. Any help would be appreciated. Thanks

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setprecision;


//function prototypes
void getTestScores (double &, double &, double &);
double calcAverage(double);
void displayAverage (double);

int main()
{
double score1 = 0.0;
double score2 = 0.0;
double score3 = 0.0;
double average = 0.0;


//call functions
getTestScores (score1, score2, score3);
calcAverage (average);
displayAverage (average);


return 0;
} //end of main function


//*****function definitions*****
void getTestScores(double &score1, double &score2, double &score3)
{
cout << "Enter first test score:";
cin >> score1;
cout << "Enter second test score:";
cin >> score2;
cout << "Enter third test score:";
cin >> score3;
} //end of getTestScores function


double calcAverage (double &score1, double &score2, double &score3)
{
double average = 0.0;
average = (score1 + score2 + score3) / 3.0;
return average;
} //end of clacAverage

void displayAverage(double &average)
{
cout << "Average is: " << average << endl;

} //end of displayAverage
Last edited on
Your problem is that once your function prototype for 'calcAverage' is different from its definition. Also, you're just calling that function ( which returns a value ) and not assigning it to anything.

Change the function call of calcAverage to:
average = calcAverage(score1, score2, score3);

Then it will work.

You should consider changing how you assign your score values though, the function 'getTestScores' isn't really using functions like you're supposed to. Arguments passed to a function usually contain a value that will be usefull to that function. You might be better off making 'getTestScores' more general and calling it 3 times within a loop from main, or just get the values from within main and ridding yourself of the function. Just something to think about.
Last edited on
What did your final program look like, just curious??
Topic archived. No new replies allowed.