Function

Trying to create a program that calculates the average of three test scores. I have to have three value-returning functions. My book also says that I will need use getTestScore() function three times. This is where I am messing up I believe. Below is what I have so far please help:

#include <iostream>
#include <iomanip>

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

//function prototype
int getTestScore();
double calcAverage(int, int, int);



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




cout << "Average:" << averageAmt << endl;
return 0;
} //end of main function


//*****function definitions*****
int getTestScore(int score1)
{
int score1 = 0;
cout << "Enter test score:";
cin >> score1;
return score1;
}
int getTestScore(int score2)
{
int score2 = 0;
cout << "Enter test score:";
cin >> score2;
return score2;
}
int getTestScore(int score3);
{
int score3 = 0;
cout << "Enter test score:";
cin >> score3;
return score3;
} //end of getTestScore function

double calcAverage (int score1, int score2, int score3)
{

double averageAmt = 0.0;
averageAmt = static_cast<double>(score1 + score2 + score3) / 3.0;
return averageAmt;

} //end of calcAverage
you really only need one gettestscore function. I think you are missing the point of the functions. They don't run after main completes, you need to call them from main like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

//function prototype
int getTestScore();
double calcAverage(int, int, int);



int main()
{
int score1 = getTestScore();
int score2 = getTestScore();
int score3 = getTestScore();
double averageAmt = calcAverage(score1, score2, score3);
cout<<"\nAverage score: "<<averageAmt;




cout << "Average:" << averageAmt << endl;
return 0;
} //end of main function


//*****function definitions*****
int getTestScore()
{
int score = 0;
cout << "\nEnter test score:";
cin >> score;
return score;
}

double calcAverage (int score1, int score2, int score3)
{

double averageAmt = 0.0;
averageAmt = static_cast<double>(score1 + score2 + score3) / 3.0;
return averageAmt;

} //end of calcAverage 
Thanks. My book does not explain that good and does not provide an example when using more than 2 functions
Topic archived. No new replies allowed.