Reading .txt file into 2D array

I need to read a .txt file as shown below. Might be hard to read, but it is shown as a 5 by 7 table. I need to input all the data from the .txt file into the array using the code below. However, only the top row is inserted and everything else is messed completely. It is completely skipping over jdark@att.net, for example. Please help.

bham@gnet.com Blake Ham squid62 1987 U Teacher
jdark@att.net Jim Dark gymrat321985 A Master
hdgreen@lakes.netHannah Green flower222007 U Apprentice
tsmith@dna.com Tom Smith tuna20 2000 U Teacher
jarrow@pnet.com James Arrow ahoy10 2005 U Apprentice

1
2
3
4
5
6
7
8
9
10
11
12
for (int x = 0; x < 6; x++)
		{
			for (int y = 0; y < 8; y++)
			{
				inputFile >> theAccounts[x][y];
			}
			cout << endl;
		}



							
If the array is 5 x 7, then the valid indices are 0,1,2,3,4 and 0,1,2,3,4,5,6. With x < 6 (up to 5) and y < 8 (up to 7), the code is going out of bounds on the array.
Even I fix this, I am having the same error
Nvm, the array is reading correctly from the .txt file now. Thank you. However, I need help sorting the array above by last name in alphabetical order. This is my current code, its does not work at all. Please help.

bool swap;
string temp;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	do
	{
		swap = false;
		for (int x = 0; x < 4; x++)
		{
			if ((theAccounts[x][1]) > (theAccounts[x + 1][1]))
			{
				temp = theAccounts[x][1];
				theAccounts[x][1] = theAccounts[x + 1][1];
				theAccounts[x][0] = theAccounts[x + 1][0];
				theAccounts[x][2] = theAccounts[x + 1][2];
				theAccounts[x][3] = theAccounts[x + 1][3];
				theAccounts[x][4] = theAccounts[x + 1][4];
				theAccounts[x + 1][1] = temp;
				swap = true;

			}
		}

	} while (swap);
You need to swap an entire row of data. Right now there's a temp variable holding just one cell of the table for swapping, the others just get overwritten (and the last two columns, 5 and 6 aren't included ?). If you're sorting by last name, the y value would be 2, not 1, I think, if this is what you want to end up with.

1
2
3
4
5
jarrow@pnet.com   James  Arrow ahoy10  2005   U Apprentice  
jdark@att.net     Jim    Dark  gymrat  321985 A Master  
hdgreen@lakes.net Hannah Green flower  222007 U Apprentice  
bham@gnet.com     Blake  Ham   squid62 1987   U Teacher  
tsmith@dna.com    Tom    Smith tuna20  2000   U Teacher 
Last edited on
Yes, thank you. This is what I want to end up with. How would I program this sort function to swap entire rows?
Ugly but works is to do the temp variable and swap for each cell in the row like you've done for the name.

Do you have to use a 2d array or can you use a struct to hold the information for each person?
Oh, yes that works. Thank you!
Topic archived. No new replies allowed.