Sort data from txt file into array as its bening read?

DrolArumil (30)
Sorry if this is too newb, but no one in beginner responded to me so ill try here.

I am suppose to make a program that reads in data from a text file (integers only) and sorts them as it inserts them into an array of size 10. I did this using an insertion sort, which worked great. But now I am being told that I need the function has to read ALL of the numbers in the text file, not just the first 10, and I am not allowed to store them THEN sort, it has to be sorted as being stored. I cannot figure out how to do this, Any help would be appreciated.

This is what I have for sorting first 10
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
void sortArray(int iArray[])
{
	string fileName = "";
	fstream inFile;
	int tmp = 0;
	int size = 10;
	
	//Prompt user to input the name of the file
	cout << "please enter file name: ";
	cin >> fileName;

	//Open file
	inFile.open(fileName);
	 int j  = 0;
	//Insertion sort, compares data as its read to the array and swaps numbers if needed
	for (int i = 0; i < size; i++)
	{
		inFile >> tmp;
		iArray[i] = tmp;

		 j = i;

		while (j > 0 && iArray[j-1] > iArray[j])
		{
			swap(iArray[j], iArray[j-1]);
			j--;
		}		
	}

		inFile.close();
}
SamuelAdams (320)
How would you do it if you were doing it on paper, or for example sorting pennies by year, one at a time ?
DrolArumil (30)
As far as I know, I have to do it one at a time, dont I? can the fstream hold more than more value at a time?

also the numbers are arbitrary, they wont necessarily be 1,2,3,4... it could be 2,5,8,12,15...

and the way the txt file is laid out is like this
1
2
3
4

so each number is a line
Last edited on
Registered users can post here. Sign in or register to post.