I/O from file in specific order

I need to input data from a file and then output it sorted two different ways: one way sorted by name with no numbers, the other way sorted by number with the name included. By "output" I mean have it show on the screen, not output to a new .txt document.

The information in the .txt for us to access should have the following information:

Luke 48

Josey 25

Jeremy 36


The final output should look like this:
Sorted by Name:

Jeremy

Josey

Luke

Sorted by Age:

Josey 25

Jeremy 36

Luke 48


I just can't seem to get it to work properly. So far what my code looks like is this:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	string Name1, Name2, Name3;
	int Age1, Age2, Age3;
	ifstream Names;
	
	Names.open("NAMES.txt");

	Names >> Name1 >> Name2 >> Name3 >> Age1 >> Age2 >> Age3;
	
	cout << "Sorted by name:" << endl << endl;
	{
		if(Name1 < Name2 && Name1 < Name3)
			cout << Name1 << endl << endl;
		else if (Name2 < Name3)
			cout << Name2 << endl << endl << Name3 << endl << endl;
	}

	cout << "Sorted by age:" << endl << endl;
	{ 
		if(Age1 < Age3 && Age2 < Age3)
			cout << Age1 << endl << endl;
		else if (Age2 < Age3)
			cout << Age2 << endl << endl << Age3 << endl << endl;
	}
	
	Names.close();

	return 0;
}


Any assistance would be greatly appreciated. I know I'm doing something wrong, I just can't figure out what.

Please note my knowledge in C++ is still not very good! I have the basics and I've aced every project so far but something with this one is really holding me up.
Last edited on
I think you want a Multidimensional array.
http://www.cplusplus.com/doc/tutorial/arrays/

Topic archived. No new replies allowed.