Loading a 1-D Array

I'm not sure what's exactly wrong with this code. We're suppose to open a file and take the numbers from that file and store it into an array, however, if a file were to have 20 values stored, this code would state that it outputted 21 elements. Is there something that I should improve?

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
string file;
	cout << "Enter the name of file: ";
	cin >> file;

	ifstream infile;

	infile.open(file.c_str());
	if(!infile)
	{
		cout << "File didn't open";
		return false;
	}

	int x = 0;
	while(infile.good())
	{
		infile >> a[x];
		x++;
	}
	
	if(infile.eof())
	{
		n = x;
		return true;
	}

	else 
	{
		cout << "Wrong datatype in file";
		return false;
perhaps because of the newline character? just subtract 1.
Thanks! It seems to have worked...dumb mistake on my part.
Topic archived. No new replies allowed.