output problem !

so i have this program that reads in 6 scores from a text file,
and prints out the average of the 6 scores and the squared average of it, using a function void and prints out the 2 averages...but my program isn't printing out anything?
why is that??


using namespace std;

int main()
{
void stat ( float , float& , float& );
cout<<"the average of the scores are: "<<endl;
cout<<"the average of the squares of the scores are: "<<endl;
system ("pause");
return 0;
}
void stat ( float scores[], float& xbar, float& x2bar )
{
ifstream indata ("anl214data.txt");
double sum=0, score;
int i;
while(i<7)
{
indata>>score;
sum+=score;
i++;
xbar=sum/6;
x2bar=pow(xbar,2)/6;
cout<<"xbar = "<<xbar<<" x2bar = "<<x2bar<<endl;
}
}


xbar is just the average
and x2bar is the average of tthe squared
Last edited on
This function prototype void stat ( float , float& , float& ); does not match the definition of the stat function void stat ( float scores[], float& xbar, float& x2bar ). The first type should be float[]. You might also want to put the function prototype above main. That way other functions can see it (I don't think having it as a local function prototype was your intent, anyway).

but my program isn't printing out anything?

In main() your print statements don't print out the average or the average of the squares. They just print "the average of the (squares of the) scores are: " and then a new line. You need to create two float variables in main and pass them in to stat. Then, you need to print them.

Also, the array float scores[] is not being used. Consider removing it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <limits> //to get rid of system("pause")
using namespace std;

void stat(float[], float&, float&); //function prototype

int main()
{
     float xbar = 0;
     float x2bar = 0;
     float anArray[2]; //this is just so the call to stat will work as is

     stat(anArray, xbar, x2bar);

     cout<<"the average of the scores are: "<<xbar<<endl;
     cout<<"the average of the squares of the scores are: "<<x2bar<<endl;

     //instead of system("pause");
     cout << "Press ENTER to continue...";
     cin.ignore(numeric_limits<streamsize>::max(), '\n');
}


Another thing is this x2bar=pow(xbar,2)/6; will not give you the average of the squares. You need to maintain a separate count for the squares and divide that count by 6 (or whatever the amount of numbers is).
Last edited on
Topic archived. No new replies allowed.