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
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
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.
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
}