Nov 3, 2013 at 9:13pm UTC
I need to build a program in which I have to write a program that asks the user how many test scores they have, and then reads the
test scores, averages them, and displays the average and a pass/fail grade appropriate for
that average (such as, “passing is 50 or higher, failing is below 50”). Use a separate
function to average the input values.
#include <iostream>
using namespace std;
double average(int numberOfCourses, float sum, float avg);
int main()
{
int numberOfCourses;
float sum = 0;
float avg = 0;
float temp = 0;
cout << "\nPlease enter number of test scores: ";
cin >> numberOfCourses;
for (int i = 0; i < numberOfCourses; i++)
{
cout << "\nPlease enter grade for test " << i + 1 << ": ";
cin >> temp;
sum += temp;
}
double average1 = average(numberOfCourses, sum, avg);
cout << "\nAverage: " << avg << endl;
if (avg >= 50)
cout << "\nPass" << endl;
else
cout << "\nFailed" << endl;
std::cin.clear();
std::cin.ignore();
getchar();
}
double average(int numberOfCourses, float sum, float avg)
{
avg = sum / numberOfCourses;
return 0;
}
The average keeps coming up 0, any help would be greatly appreciated.
Nov 3, 2013 at 9:27pm UTC
Try this (not 100% sure this will work)
1 2 3 4 5
double average(int numberOfCourses, float sum, float &avg)
{
avg = sum / float (numberOfCourses);
return 0;//PS: your func returns 0 anyway
}
Has the compiler given any warning?
Last edited on Nov 3, 2013 at 9:29pm UTC
Nov 3, 2013 at 9:35pm UTC
Okay well when I take out the 0 it says I must have something to return so what would I put? I am in new to computer science and C++
Nov 4, 2013 at 6:31am UTC
A function can return any type of value, not only 0.
Try something like that un this case:
return sum / float (numberOfCourses);