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>
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.
#include <iostream>
usingnamespace 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.
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;
}
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.