Program not reading file correctly because of while loop

My program is all running fine except that it is reading a seemingly random seven-digit number at the end of the file to add an additional number to the file and I have not been able to figure out why. I was able to determine that it has something to do with the first while loop.

I have tried everything I can think of and one thing I have noticed is that when I change the global variable to the number of numbers I know are in the file (12) it will work perfectly. However, I am suppose to do this so that I do not know exactly how many numbers are in the file.

What the output in the new file is suppose to look like but with the extra number:
8
10
12
17
25
36
47
62
62
65
87
89
2972880 //However this last seven digit number is not suppose to be there...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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>

using namespace std;

const int SIZE = 256;

int main(void)
{
	int getNumbers[SIZE];
	ofstream outFile;
	ifstream inFile;
	bool success;
	int count = 0,
		total = 0,
		add = 1;
	int	num;
	
	inFile.open("numbers.txt");
	
	if (inFile.is_open())
	{
		cout << "File successfully read." << endl;
	}
	
	while (count < (total + add) && !inFile.eof())
	{
		inFile >> getNumbers[count];
		total += add;
		count++;
	}
	
	int grandTotal = (total - 1);
	
	do
	{
		success = false;
		for (int i = 0; i < grandTotal; i++)
		{
			if (getNumbers[i] > getNumbers[i + 1])
			{
				num = getNumbers[i];
				getNumbers[i] = getNumbers[i + 1];
				getNumbers[i + 1] = num;
				success = true;
			}
		}
		grandTotal--;
		
	} while (success);
	
	inFile.close();
	
	outFile.open("sorted.txt");
	
	if (outFile.is_open())
	{
		cout << "File successfully opened." << endl;
	}
	
	int index = 0;
	while (index < total)
	{
		outFile << getNumbers[index]
			    << " \n";
		index++;
	}
	
	outFile.close();
	
	return 0;
}
Hello bigzigzag. Your code actually worked correctly for me, it sorted the numbers with out adding any extra. My guess is that maybe your input file had a non-number in it. Try deleting your input file and remake it, just to be safe. If that doesn't work, maybe try having a little loop to go through your number array and clear all the numbers to 0 at the beginning of your code. Thank :D
Really??? I don't understand because I even tried saving it to a new file.
Topic archived. No new replies allowed.