arrays and strings

Can some one help me with this.I have a project and I have a file with the list off all the presidents from Washington to Obama it looks like
WASHINGTON,GEORGE
ADAMS,JOHN
JEFFERSON,THOMAS
MADISON,JAMES
MONROE,JAMES
ADAMS,JOHN_QUINCY
and so on.

What I need to do is make a list that shows the name of the president. Which number they were in order and which president is next. for example

0 2 adams,John 22

the 22 so it can show which president is next by that first column and and the first one is just in order from 0-50. This is what I have

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
#include<fstream>
#include<iostream>
#include<cstring>
using namespace std;

int main()
{

	struct president
	{
		int location;
		int chronom;
		char pname[25];
		int next;
	}; 


	president *plist[50] = { NULL };
	president *temp;
	int i;
	int rightmost;
	int imax;
	ifstream fin;
	ofstream fout;

	fin.open("prez.dat");
	fout.open("list.txt");
	int n=0;

	while(!fin.eof())
	{

		plist[n] = new president();
		fin>>plist[n]->location;
		fin>>plist[n]->chronom;
		fin>>plist[n]->pname;
		fin>>plist[n]->next;
		n++;
	}
	for(rightmost=n-1;rightmost>0;rightmost--)
	{
		imax=0;
		for(i=1;i<=rightmost;i++)
			if(strcmp(plist[i]->pname,plist[imax]->pname)>0)
				imax=i;
		temp = plist[imax];
		plist[imax]=plist[rightmost];
		plist[rightmost]=temp;
	}

	fout<<"location"<<"\t"<<"chronum"<<"\t"<<"president's name"<<"\t"<<"next prez"<<endl<<endl;


	for (int i = 0; plist[i] != NULL && i < 50; i++) {
		fout << i<<plist[i]->location << " " << plist[i]->chronom << " " << plist[i]->pname << " " << plist[i]->next << endl;
	}
	return 0;
}



can some one help me with the output? im not getting anything. When I debug it, it shows nothing being taken in. I am using cstyle string. We can change if need be.
Last edited on
Hi

You haven't read a line in from the file. You need to do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	fin.open("prez.dat");
	fout.open("list.txt");
	int n=0;

        string line;

	while(!fin.eof())
	{
                std::getline (fin, line);
		plist[n] = new president();

                // parse the line variable by whatever it is delimited by
                // and then place these into the plist[n] structure

		n++;
	}
Topic archived. No new replies allowed.