What is wrong?? Need Help


Hello my objective for this program is to take a data file for input
and find the total numbers on the field, the average, and the sum
and output it to another file.

this is what i have but for some reason it is not working..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77


#include<iostream>
#include<fstream>



using namespace std;

int main()
{
	float numOfData = 0;
	float totalVal = 0;
	float curVal;
	float average;
	int lowest = 0;
	int highest = 0;



ofstream outNewFile; // declaring the output file
outNewFile.open("Lab5.dat", ios::out);  // opening outfile

if(!outNewFile.fail()) // checks for outfile open success
{

	ifstream inOldFile; // Declaring the input file

	inOldFile.open ("C:\\***\\***\\***\\record.txt", ios::in); // opening the input file
	

	if(!inOldFile.fail())  // checking for inputfile open success
	{
	
		inOldFile >> curVal;  // Writes the data from input to variable Current Value
		
		
		while(curVal!=inOldFile.eof())
		{

			while(curVal != '\n')
			{
			
				numOfData = numOfData + 1;
			totalVal = curVal + totalVal;
			average = totalVal / numOfData;
			inOldFile >> curVal; 
		
			}

		cout<< numOfData <<endl;
			cout<< totalVal<<endl;
		cout<<average<<endl;
 cout<<"success"<<endl;
	system("PAUSE");
	inOldFile >> curVal;
		}
			inOldFile.close();
	}
	else
	{
	cout<<"fail"<<endl;
		system("PAUSE");
	}
 
	
}

else
{
	cout<<"Fail"<<endl;

}
outNewFile.close();
}



i end up getting

55 for num of data
and 21113 for totalVal
and it wont cout average for some reason.



it wont cout average for some reason
Well, I can't see anything that could cause that, but you should know that the way you're calculating the average is incorrect. You're giving much more weight to the first values than to the last ones.
I am not sure I am following...

totalVal has all the numbers added up
and numOfData has the amount of numbers I added up. Wouldn't the average be the total divided by the amount numbers added up?
Never mind. I thought because it was inside the loop you were doing something else, which is a common mistake.
Yes, the way you're doing it will give the correct result, but there no reason why it needs to be inside the loop.
The thing is... It is not giving me correct results.

In fact, if i change the initial statements

numOfData = 0; to numOfData = 575;

It will produce the same numbers if I hadn't changed them.

Anyone???
Topic archived. No new replies allowed.