C++ Arrays Highest, Lowest, and Mean

Can anyone find out the problem with this coding?

#include <iostream>
using namespace std;

void printGrades(int *a, int len)
{
if(len == 0) cout << "\nNo students entered";
	else
	{
		cout << "\nNumber\tGrade";
		cout << "\n-------- -----\n";
		for (int n=0; cout << n+1 << "\t" << a[n] << endl;);
	}
}

float findMean(int *a, int len)
{
float total = 0;
for (int n=0; total += a[n];)
return(total / len);
}

int isHighest(int *a, int len)
{
int high = a[0];
for (int n=0; n;)
{
if(n > 0)
if(high < a[n])
high = a[n];
}
return high;
}

int isLowest(int *a, int len)
{
int low = a[0];
for (int n=0; n;)
{
if(n > 0)
if(low > a[n])
low = a[n];
}
return low;
}

int main()
{
int *grades = NULL; //array to hold grades
int input, n = 0;

while(1) //Builds dynamic array until the user enters a negative
{
cout << "Enter student " << n + 1 << "'s grade (negative to stop): ";
cin >> input;
if (input < 0) break;
grades = (int *) realloc (grades, (n+1) * sizeof(int));
grades[n++] = input;
}

printGrades(grades, n);
cout << "\nClass mean is " << findMean(grades, n); //output of methods
cout << "\nHighest grade is " << isHighest(grades, n);
cout << "\nLowest grade is " << isLowest(grades, n);

char ans; //ending dialog
cout << "\n\nDo you want to enter another class' scores? (y/n) ";
cin >> ans;
if(ans == 'y' || ans == 'Y')
{
main();
cout << "\n\n\n\n";
}

free(grades);
system("pause");
return 0;
}


I end up with this error:
c:\users\user\documents\visual studio 2010\projects\assignment 4\assignment 4\assignment 4.cpp(20): warning C4715: 'findMean' : not all control paths return a value

Thanks for reading and hope this can be resolved.
Last edited on
closed account (zb0S216C)
Since the error is referring to findMean( ), it's best to start looking there. basically, the error is telling your that you're missing a return statement. When a statement leaves out the braces, it's allowed only one statement as it's body. For example:

1
2
while( true )
    std::cout << "Printing..." << std::endl;

In your findMean( ) function, your for loop uses the return statement as it's body. That's whats causing the error. You're going to have to add another statement such as cout or something for the body of the for loop.

Wazzak
Thanks for the help, got the error to end and the loop to stop. Just need to work on some other problems.
Topic archived. No new replies allowed.