Help with reading a file to an array

Hello,

I'm working on some code for school and ran into an issue. The assignment is as follows:
"Write a program which reads the numbers.txt file (available in BlackBoard in the Week 2 folder). The file contains a series of numbers, each written on a separate line. The program will read the contents of the file into an array and then display the following information:

the lowest number in the array
the highest number in the array
the total of the numbers in the array
the average of the numbers in the array."

So I have my code to read the file which has 12 random numbers listed out, each on a new line, when I execute the code, it lists 12 numbers, the first is actually the last number in the list and the rest are 858993460 which is obviously not correct. How do I get it to read the rest of the numbers properly? Once I get that worked out I know how to get the rest of the parts of the assignment 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
  #include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;


int main()
{ 
	const int ARRAY_SIZE = 12;
	int numbers[ARRAY_SIZE];
	int count = 0;
	ifstream inputFile;

	inputFile.open("C:\\users\\rocke\\desktop\\numbers.txt");

	while (count < ARRAY_SIZE && inputFile >> numbers[count]);
		count++;

	inputFile.close();

	cout << "The numbers are: " << endl;
	for (count = 0; count < ARRAY_SIZE; count++)
		cout << numbers[count] << " " << endl;
	
    return 0;
}
Line 17. The while loop body (the bit that will be repeated) runs from the closing ) to the ;. Line 18 is not inside the while loop body.

Remove the semi-colon, and USE BRACES around your while loop body. I don't care if it's only one line. USE BRACES.
Hello cjmay2013,

As you pointed out:
the first is actually the last number in the list and the rest are 858993460

This is because the array was not initialized when it was defined, so when the compiler sets up memory for the array and the program runs all you have is the garbage that is still hanging around at that memory location for that block. As Repeater pointed out "count" never changes, so all the reads are going into element zero. When element zero contains the last number read and the rest of the array is something else it is good indication the something is wrong with the way it is reading the file.

It is always good practice and programming to initialize your variables when you define them.

I like the uniform initializer of {}s. The empty {}s will initialize numeric variables to zero or 0.0 for floats and doubles and char to '\0'. The exception is std::string which is empty to start with. You can also put numbers or strings inside the {}s. For an array it will initialize all the elements to zeros.

Hope that helps,

Andy
Thank you both so much! I took my first programming class over a year ago and then had a bunch of business classes in the semesters between that class and my current class, so a lot of things like that are going over my head still while I try to refresh my memory.
The code works perfectly now.

Thanks again!
Topic archived. No new replies allowed.