struct help

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

int main (void)
{
	
	struct {int chrono=0; string pname; int next;}plist[50],temp;
	int n = 0,bottom,maxindex,i,m,location[50]={0};
	
	ifstream fin;
	fin.open("prez.txt");
	
	fin>>plist[n].pname;
	
	while(!fin.eof())
	{
		plist[n].chrono=n+1;
		n++;
		fin>>plist[n].pname;
		
	}
	
	for(bottom = n-1; bottom > 0; bottom--)
	{
		maxindex = 0;
			for (i=0; i<=bottom; i++)
				if (plist[i].pname > plist[maxindex].pname)
					maxindex=i;
				temp=plist[maxindex];
				plist[maxindex]=plist[bottom];
				plist[bottom]=temp;
	}

		
	for (n=0;n<50;n++)
	cout<<n<<"      "<<plist[n].chrono<<setw(30)<<plist[n].pname<<setw(20)<<plist[n].next<<endl;
}



Location     Prez number           Prez name           Next pres
0      2                    ADAMS,JOHN            11618240
1      6             ADAMS,JOHN_QUINCY             4750496
2      21                ARTHUR,CHESTER                   0
3      15                BUCHANAN,JAMES             4747952
4      41                   BUSH,GEORGE             2358272
5      43            BUSH,GEORGE_WALKER             2358464
6      39                  CARTER,JIMMY                   1
7      22              CLEVELAND,GROVER                   0
8      24             CLEVELAND2,GROVER                   0
9      42               CLINTON,WILLIAM             4238979
10      30               COOLIDGE,CALVIN             4296148
11      34             EISENHOWER,DWIGHT                   0
12      13              FILLMORE,MILLARD             2358112
13      38                   FORD,GERALD                   6
14      20                GARFIELD,JAMES                   0
15      18                 GRANT,ULYSSES                   0
16      29                HARDING,WARREN             2358176
17      23             HARRISON,BENJAMIN                   0
18      9        HARRISON,WILLIAM_HENRY                   0
19      19              HAYES,RUTHERFORD                   0
20      31                HOOVER,HERBERT                   1
21      7                JACKSON,ANDREW             4750496
22      3              JEFFERSON,THOMAS            11600752
23      17                JOHNSON,ANDREW                   2
24      36                JOHNSON,LYNDON             4683621
25      35                  KENNEDY,JOHN             4789056
26      16               LINCOLN,ABRAHAM             4675425
27      4                 MADISON,JAMES                  12
28      25              MCKINLEY,WILLIAM             4791680
29      5                  MONROE,JAMES            11473696
30      37                 NIXON,RICHARD             4788560
31      44                  OBAMA,BARACK             2358608
32      14               PIERCE,FRANKLIN                 255
33      11                    POLK,JAMES          -179216513
34      40                 REAGAN,RONALD            11473696
35      32            ROOSEVELT,FRANKLIN             4788560
36      26            ROOSEVELT,THEODORE            11473776
37      27           TAFT,WILLIAM_HOWARD             4791680
38      12                TAYLOR,ZACHARY             4788536
39      33                  TRUMAN,HARRY             4796224
40      10                    TYLER,JOHN                  28
41      8              VAN_BUREN,MARTIN             4674813
42      1             WASHINGTON,GEORGE           268435729
43      28                WILSON,WOODROW             4791888
44      0                                          11473696
45      0                                           4750496
46      0                                                 0
47      0                                        1841533212
48      0                                             65535
49      0                                                 0


The whole objective in the next column is to find the president that succeeded the prez. so like john adams was succeeded by Thomas Jefferson. but Jefferson is in like 22. I can not figure out how to make the next column find Jefferson and then put what line he is in in adams next box

so it would be like

0 2 Adams 22


PLESASE HELP I WILL CHECK THIS FOURM THOROUGHOUT THE DAY AND WILL RESPOND TO ANY CONFUSION.

Don't loop on EOF.

1
2
3
4
5
while (fin>>plist[n].name)
{
  plist[n].chrono=n;
  n++;
}

Now, after sorting, when you look up element 22 (Jefferson), the chrono member says he was the third president. The president after him will have a chronon number of 4. Find the index of the element that has chrono==4. That's your next.

Do that for every element, in order.


Must you use a struct like this? It would be much more efficient to simply store a lookup table and sort from that.
so how would you call it. I understand what you are saying but how would you call

plist[n].chrono to find the other chrono and the switch the next. I know that is how it would work but I couldn't figure out how to get the chrono to find the other chrono
Find the index of the element that has chrono==4.

You'll need a handy function to find the index of an element with a specific chrono value.

You'll probably want to name your struct type, too.

1
2
3
4
5
6
7
struct president_t 
{
	int chrono; 
	string pname; 
	int next;
	president_t(): chrono( 0 ), next( -1 ) { }
};
1
2
3
4
5
int find_president( president_t* plist, int size, int chrono )
{
  // find the index of the element in plist where 
  //   (plist[n].chrono == chrono)
}
1
2
3
4
5
int main()
{
	president_t plist[50], temp;
	...
}

Good luck!
Topic archived. No new replies allowed.