C++ Exercise

Hi. I am relatively new to programming and need help with a problem from my book.
It is:

In this exercise, you create a program that calculates the average of three test scores. The program should contain three value-returning functions: main, getTestScore, and calcAverage. The main function should call the getTestScore function to get and return each of three test scores. The test scores may contain a decimal place. (Hint: The main funciton will need to call the getTestScore function three times.) The main function then should call the calcAverage function to calculate and return the average of the three test scores. When the calcAverage function has completed its task, the main function should display the average on the screen. Display the average with one decimal place.

I'm not sure if you're supposed to use an array to collect scores, have separate score variables, or increment all the scores into one total sum before sending to the average function. Or if you've covered loops?

for the number of scores to be collected (loop)
---score = result of getTestScore function

average = the result of calcAverage function
print average
THis is what I already have but it isn't working....we are working on value-returning functions. We have covered loops but my teacher said this does not need a loop. I am extremely lost on this one.
#include <iostream>

double getTestScore( double scores);
double calcAverage( double scores);

using namespace std;

int main()
{
//declare variables
double score1 = 0.0;
double score2 = 0.0;
double score3 = 0.0;
double testScore = 0.0;
double average = 0.0;

//enter input
cout << "Please enter first score: ";
cin >> score1;
score1 = getTestScore(testScore);
cout << "Please enter second score: ";
cin >> score2;
score2 = getTestScore(testScore);
cout << "Please enter third score: ";
cin >> score3;
score3 = getTestScore(testScore);

average = calcAverage(testScore);

//display the total points and grade
cout << "Average is: " << average << endl;

return score1;
return score2;
return score3;

system("pause");
return 0;
} //end of main function

double calcAverage(double scores)
{
double calculatedAverage = 0.0;
double getTestScore( double scores);

//calculate average
calculatedAverage = (scores) / 3;

return calculatedAverage;

} //end of calcAverage function
Now I am trying out this:

#include <iostream>

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

using namespace std;

int main()
{
//declare variables
int score1 = 0;
int score2 = 0;
int score3 = 0;
double average = 0.0;
double score = 0.0;

//get input items
cout << "First test score: ";
cin >> score1;
cout << "Second test score: ";
cin >> score2;
cout << "Third test score: ";
cin >> score3;

//call function to calculate payments
average = calcAverage (score1,score2,score3);
cout << "Average: " << average << endl;

//call function to get test scores
score = getTestScores (score1,score2,score3);



system("pause");
return 0;
} //end of main function

//function definitions
double getTestScore (int number1, int number2, int number3)
{
return number1;
return number2;
return number3;
} //end of getTestScore

//function definitions
double calcAverage (int num1, int num2, int num3)
{
double avg = 0.0;
avg = static_cast<double>(num1 + num2 + num3) / 3.0;


return avg;
} //end of calcAverage function

However, it says I have 1 unresolved externals....
You need to define the getTestScore function outside of the calcAverage function. And you shouldn't be trying to ask the user for input in main or return score1, score2 and score3 from main. The getTestScore function is where you get the input and return a score from there to main.

You don't really need a new variable in the average function. You can just return the result of the calculation.

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
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

double getTestScore();
double calcAverage( double scores);

int main()
{
    //declare variables
    double score1 = 0.0;
    double score2 = 0.0;
    double score3 = 0.0;
    double testScore = 0.0;
    double average = 0.0;
    
    //enter input
    score1 = getTestScore();
    score2 = getTestScore();
    score3 = getTestScore();
    
    //calculate total of the three scores and save in testScore
    //code here
    
    average = calcAverage(testScore);
    
    //display the total points and grade
    cout << "Average is: " << average << endl;
    
    return 0;
} //end of main function

double getTestScore()
{
    //collect one score from user
    //return to main
}//end of getTestScore function

double calcAverage(double scores)
{
    double calculatedAverage = 0.0;

    //calculate average
    calculatedAverage = (scores) / 3;
    
    return calculatedAverage;
    

} //end of calcAverage function  
When I do that code it says the getTestScores needs to return a value. Would I do that under double getTestScore? Because in the prompt it says the main funciton needs to return each of the three test scores.
I didn't complete all the code in the example I posted.

call the getTestScore function to get and return each of three test scores


the getTestScore function should get the input and return it. You can only return one value from a function, so you need to call the function 3 times.

I am just a little unsure exactly how to do that. I am having trouble calling functions.
Here's a general example of a function returning input to a variable in main. Note in this case, main isn't sending any information to the function. In your calcAverage function you will be sending the total score value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main
{
    //
    //call getInput function, store value returned in input variable
    input = getInput();
    //
}

double getInput()
{
    double input;
    cout << "Enter something: ";
    cin >> input;
    return input;
}
#include <iostream>
using namespace std;

double getTestScore();
double calcAverage( double scores);

int main()
{
//declare variables
double score1 = 0.0;
double score2 = 0.0;
double score3 = 0.0;
double testScore = 0.0;
double average = 0.0;

//enter input
score1 = getTestScore();
score2 = getTestScore();
score3 = getTestScore();

//calculate total of the three scores
testScore = score1 + score2 + score3;

average = calcAverage(testScore);

//display the total points and grade
cout << "Average is: " << average << endl;

return 0;
} //end of main function

double getTestScore()
{
double score1;
double score2;
double score3;

cout << "First Score: ";
cin >> score1;
cout << "Second Score: ";
cin >> score2;
cout << "Third Score: ";
cin >> score3;

return score1;
return score2;
return score3;

//return to main
}//end of getTestScore function

double calcAverage(double scores)
{
double calculatedAverage = 0.0;

//calculate average
calculatedAverage = (scores) / 3;

return calculatedAverage;


} //end of calcAverage function


Now I have this but it won't calculate the average. It just repeats the cout statements over and over.
The main function should call the getTestScore function to get and return each of three test scores. The test scores may contain a decimal place. (Hint: The main funciton will need to call the getTestScore function three times.)


The getTestScore function is going to get one score at a time, not all three at the same time. You're going to call the function three times. Get one score, return one score. You're calling it three times now, but trying to get three scores each time (a total of 9 scores)


You can't return multiple values like this from a function. The first return statement is going to terminate the function.
1
2
3
return score1;
return score2;
return score3;
Thank you very much for your help and patience with my lack of programming skills!!
Topic archived. No new replies allowed.