I was asked "This program asks for a series of five test scores and calculates the average score in a function. The function should receive the total of the five scores as an argument and return the average of the five scores. The program should not accept scores less than zero or greater than one hundred. The output should look like the example below."
Here's my code. It works, but I think I could do WAY better. Could anobody nudge me in the right direction? I really need to check each input as is comes in, but can't figure out a way to do that.
BTW I'm really green with C++ so be gentle. ;)
#include<iostream>
#include<string>
using namespace std;
int average(int a, int b, int c, int d, int e); // prototype average
int main()
{
// declare variables
int testscore = 0;
int iaverage = 0;
int f = 0;
int g = 0;
int h = 0;
int i = 0;
int j = 0;
// get input
cout << "Please enter 5 test scores ";
cin >> f >> g >> h >> i >> j;
if(f <= 0 || f > 100)
{
cout << "Please enter a value between 0 - 100";
cout << endl;
}
else
{
iaverage = average(f, g, h, i, j);
testscore = (f+g+h+i+j);
cout << "Total of all scores: " << testscore << endl;
cout << "Average score: " << iaverage << endl;
}//end if-else
return 0;
}//end main
int average(int a, int b, int c, int d, int e)
{
int daverage;
daverage = (a+b+c+d+e) / 5;
return daverage;
} //end average