Inputting numbers from text file into an array

So i am writing a program that is supposed to take numbers from a text file and input them into an array. The array then proceeds to get the average and find the highest number of the inputted numbers from the text file.

The code i wrote for finding the maximum and finding the average work when i put the inputs inside the program as array[100] = {1, 5 ...etc. but when i use the text file with the same inputs i get the wrong answer. So i was wondering if you guys could show me how to fix the problem. Also is there a way to make my array conform to the size of the text file i.e. if the text file has six numbers the array has six openings or am i stuck putting in an arbitrary number in like 100.

the text file has the inputs 1 5 6 8 36 9 25
when the program compiles i get -9.25596e+061 for the average and maximum

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
double average(double * a, int a_size)
{
	double sum = 0;

	for (int i = 0; i < a_size; i++)
	{
		sum += *(a++);
	}

	return sum / a_size;
}

double * maximum(double * a, int a_size)
{
	double *temp = a;
	for (int i = 1; i < a_size; i++)
	{
		if (*temp < *(a + i))
		{
			*temp = *(a + i);
		}
	}

	return temp;
}
int main()
  {
	double array[100];
	int a_size = 0;
	ifstream fin;
	fin.open("InputFile.txt", ios::in);
	while (!fin.eof())
	{
		fin >> array[100];
		cout << array[100] << endl;
		a_size++;
	}
cout << a_size << endl;

	cout << average(array, a_size) << endl;;
	cout << *maximum(array, a_size) << endl;

Last edited on
do you know what is going on here?

1
2
3
4
5
6
while (!fin.eof())
{
	fin >> array[100];
	cout << array[100] << endl;
	a_size++;
}


index of an array element can be 0 - 99, so you're playing with not-allocated memory
... and moreover - you're still rewriting only one value - array[100]
use another variable to be the index of your array

1
2
3
4
5
6
7
8
int index = 0;
while (!fin.eof())
{
	fin >> array[index];
	cout << array[index] << endl; // this will print that extra number too
	a_size++;
        index++;
}


plus - when EOF indicator is set, loop iterates once more, which results in one extra number and a_size is also incremented by one
decrementing them after for() loop should help
1
2
--index;
--a_size;
Last edited on
Thanks for the help. I was able to get it to work. Now that i see what was wrong i feel stupid, at least i got the pointers right.
Um... you shouldn't loop on EOF at all -- you still have the potential for indexing errors -- either an off-by-one or (as you have) a potential off-by-a-whole-lot.

1
2
3
4
5
6
7
8
9
10
11
12
const unsigned a_max_size = 100;
double array[ a_max_size ];

...

unsigned a_size = 0;
while (fin >> array[ a_size ])
{
  ++a_size;
  if (a_size == a_max_size)
    break;
}

No potential buffer overruns, and your indices are always correct.

Hope this helps.
Thanks for the advise. I'll try to avoid using eof in a loop in the future, though in the assignment i am supposed to implement numbers until end-of-file (excerpt*: To test the correctness of the two functions, write your own main() to enter a list of numbers until end-of-file, then call each function and print the average and the maximum.). So is there a better way to include eof in my program so it won't have indexing errors?
The while (fin >> x) is the correct way to handle EOF.

As long as the input stream (fin) is in a usable state (not in error and not at EOF), then it will attempt to read and translate a value into the target variable (x). If that fails for any reason (such as hitting EOF), then the stream is marked as unusable, and the while condition will test it as such and stop.

A longer way of writing it is:

1
2
3
4
5
6
while (true)
{
  fin >> array[ a_size ];
  if (!fin) break;
  ...
}

Again, the reason for the bad state may be EOF or some other failure (such as trying to read an integer when the next item in the input is "hello"). If you want to check that it is, in fact, EOF, you can do that after the loop:

1
2
3
4
5
6
7
8
9
10
11
while (fin >> array[ a_size ])
{
  ...
}

if (!fin.eof())
{
  cerr << "I found bad data (not an integer) in the input file.\n"
          "(Hence, the entire input file was not read.)\n";
  // your code can quit the program here, if you like, or just continue on anyway
}

Hope this helps.
Thanks for the help. I got the program running nicely now and learned quite a bit too. Hopefully i get a good grade. Thanks
Topic archived. No new replies allowed.