reading data from a file using classes

I have to read in the first name last name and age of a person from an input file that reads:
Moses Horwitz 77
Moe Howard 77
Samuel Horwitz 60
Shemp Howard 60
Jerome Horwitz 48
Curly Howard 48
Louis Feinberg 72
Larry Fine 72

I need to store the data in an array and display it. Then I need to sort the people by age and display the array again.
I am able to get the array to display the first time, but cannot get the program to display the sorted array. Please help me. I've been working on this for hours with no luck.
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  #include<iostream>
#include<fstream>
#include<iomanip>
#include<string>

using namespace std;

class my_class
{
public:
	void store_info(string f, string l, int a);
	void retrieve_info(string& f, string& l, int& a);
	int get_age();
	my_class();
	my_class(string f, string l, int i);
private:
	string f_name;
	string l_name;
	int age;
};

void my_class::store_info(string f, string l, int a) 
{
	f_name = f;
	l_name = l;
	age = a;
}

void my_class::retrieve_info(string& f, string& l, int& a) 
{
	cout << f << " " << l << " " << a << endl;
}

int my_class::get_age()
{
	return age;
}

my_class::my_class()
{

}

my_class::my_class(string f, string l, int i)
{
	store_info(f, l, i);
}

void sort(int size, my_class array[]);

int main()
{
	ifstream infile;
	string fname, lname;
	int i =0, age, x;
	infile.open("class_data_dos.txt");
	my_class peeps[10];

	infile >> fname >> lname >> age;
	while (!infile.eof())
	{
		i++;
		for (x = 0; x < i; x++)
		{
			peeps[x].store_info(fname, lname, age);
		}
		peeps[i].retrieve_info(fname, lname, age);
		infile >> fname >> lname >> age;
	}

	cout << endl;
	sort(i, peeps);
	peeps[i].retrieve_info(fname, lname, age);
	
	return 0;
}


void sort(int size, my_class array[])
{
	int x;
	my_class temp;

	for (int i = 0; i < (size - 1); i++)
	{
		x = i;

		for (int j = i; j < size; j++)
		{
			if (array[j].get_age() < array[x].get_age())
			{
				x = j;
			}
		}
		if (x != i)
		{
			temp = array[x];
			array[x] = array[i];
			array[i] = temp;
		}
	}
}

my output:
Moses Horwitz 77
Moe Howard 77
Samuel Horwitz 60
Shemp Howard 60
Jerome Horwitz 48
Curly Howard 48
Louis Feinberg 72
Larry Fine 72

Larry Fine 72
Hello mdelellis,

Normally I would complain about the while condition using "eof", but you have done it in a way that works.

Now inside the while loop you do not need the for loop. That is working against you. To make it short:
1
2
3
4
5
6
7
8
9
10
while (!infile.eof())
{
	peeps[i].store_info(fname, lname, age);  // <--- Stores in array.

	peeps[i].retrieve_info(fname, lname, age);  // <--- Prints from what was read in from the file not the array.

	i++;

	infile >> fname >> lname >> age;
}


Hope that helps,

Andy

The only other thing I did in main was to add this just before the return.
1
2
3
4
5
6
7
for (size_t lc = 0; lc < i; lc++)
{
	peeps[lc].Print();
}

std::cout << "\n\n Press Enter to continue";
std::cin.get();

The last two lines I need to keep the console window open. And I had to create the member function "Print":
1
2
3
4
void my_class::Print()
{
	std::cout << f_name << ' ' << l_name << ' ' << age << std::endl;
}


The sort function was all wrong. I still have not figured out what you are trying to do. This is all you need for the sort function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void sort(int size, my_class array[])
{
	my_class temp;

	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size - 1; j++)
		{
			if (array[j].get_age() < array[j + 1].get_age())  // <--- < decending.  > ascending.
			{
				//x = j;
				temp = array[j];
				array[j] = array[j + 1];
				array[j + 1] = temp;
			}
		}
	}
}


Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.