Reading from file to an array

I am having problems with my array keeping the values that I assign it from my text file. Below is my text file
87453
35897
28976
76345
98723
13498
56783
67345
87345
13401


Here is the code:
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
#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

const int SIZE = 10;
void ReadCodes(int codes[]);

int main()
{
    int codes[SIZE];
    double prices[SIZE];
    char cont;
    
    ReadCodes(codes);	// cout diretly from the function

cout << "\n"; // 

for (int count = 0; count < SIZE; count++)
	{
		cout << codes[count] << endl; //cout after function
	}
             
    return 0;
}

void ReadCodes(int codes[])
{
	//File stream object
	ifstream inputFile;
	//Open the txt file
	inputFile.open("ProductCodes.txt");

	//Test for errors and then read the file
	if (!inputFile)
	{
		cout << "Error opening the file. \n";
	}
	else
	{
		 for(int sub=0; sub<SIZE; sub++)
		 {
			while (inputFile.eof() == false)  //Test for end of file
			{
				int number = 0;
				inputFile >> number;	//Read a number
				codes[sub] = number;
				cout << codes[sub] << endl;
			}
		 }

	inputFile.close();
	}
}


This is what the code is outputting:
87453
35897
28976
76345
98723
13498
56783
67345
87345
13401

13401
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460


Continue (Y/N)


It also seems like it is going in the reverse order...? Thanks for your help
Last edited on
your last numbers are probably too big, use a while loop instead of a do...while, it should fix
I just edited the post, but the do..while was for a different function. Now look at it and tell me what you think. Thanks
the program is outputting error codes, i think, look to see the pattern, all are relitively the same
The problem is here:

1
2
3
4
5
6
7
8
9
10
for(int sub=0; sub<SIZE; sub++)
{
    while (inputFile.eof() == false)  //Test for end of file
    {
        int number = 0;
        inputFile >> number;	//Read a number
        codes[sub] = number;
        cout << codes[sub] << endl;
    }
}

That while loop simply overwrites codes[0] until it reaches the end-of-file. The other elements in the array are never initialized.
Ahh I see what you are saying, because I do have the for loop, but the while loop doesn't end until the end of the file. How could I go about fixing this? As I need both loops right?
Not really. I'd read the file like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
int n;
for(int i = 0; i < SIZE; ++i)
{
    if(inputFile >> n)
    {
        codes[i] = n;
        std::cout << codes[i] << std::endl;
    }
    else
    {
        // the stream is either in a fail(), bad() or eof() state; deal with error
    }
}

Actually, I'd use a std::vector instead of an array, as it's easier to use and much more flexible, but I'm assuming this is an exercise on arrays.
Last edited on
Yup, trying to get familiarized with arrays. Thank you so much for the help.
Topic archived. No new replies allowed.