Pointers

I am writing this program that needs a pointer. From the output below, I need to have the NEXT column point to the next president successor and show that president's alphabetical order number. So for example, for John Adams, the NEXT number needs to be 22 (who represents Thomas Jefferson). Note: It needs to be efficient (in other words, it doesn't need to run through all 44 presidents each time). Also, there needs to be a solution for Obama since there is no successor yet.

I don't know where to start on this one. Thanks in advance for those who 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
41
42
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

void main()
{
  struct{int chronum; char pname[25]; int next;} plist[50], temp;
  int n=0, i, end, maxindex;

  ifstream fin;
  ofstream fout;
  fin.open("prez.dat");
  fin >> plist[n].pname;
  fout.open("output.dat");

  fout << right << setw(8) << "Alpha #" <<  setw(12) << "Chronum" << setw(25) << "Pres. Name" << setw(8) << "Next" << endl << endl;

  while(!fin.eof())
  {
    plist[n].chronum = n+1;
    n++;
    fin >> plist[n].pname;
  }
  
  for(end=n-1; end>0; end--)
  {
    maxindex = 0;
    for(i=0; i<=end; i++)
    {
      if(strcmp(plist[i].pname, plist[maxindex].pname)>0)
        maxindex=i;
      temp = plist[maxindex];
      plist[maxindex]=plist[end];
      plist[end]=temp;
    }
  }

  for(i=0; i<=43; i++)
    fout << right << setw(8) << i << setw(12) << plist[i].chronum << setw(25) << plist[i].pname << setw(8) << /*plist[i].next <<*/ endl;
}


 Alpha #     Chronum               Pres. Name    Next

       0           2               ADAMS,JOHN
       1           6        ADAMS,JOHN_QUINCY
       2          21           ARTHUR,CHESTER
       3          15           BUCHANAN,JAMES
       4          41              BUSH,GEORGE
       5          43       BUSH,GEORGE_WALKER
       6          39             CARTER,JIMMY
       7          22         CLEVELAND,GROVER
       8          24        CLEVELAND2,GROVER
       9          42          CLINTON,WILLIAM
      10          30          COOLIDGE,CALVIN
      11          34        EISENHOWER,DWIGHT
      12          13         FILLMORE,MILLARD
      13          38              FORD,GERALD
      14          20           GARFIELD,JAMES
      15          18            GRANT,ULYSSES
      16          29           HARDING,WARREN
      17          23        HARRISON,BENJAMIN
      18           9   HARRISON,WILLIAM_HENRY
      19          19         HAYES,RUTHERFORD
      20          31           HOOVER,HERBERT
      21           7           JACKSON,ANDREW
      22           3         JEFFERSON,THOMAS
      23          17           JOHNSON,ANDREW
      24          36           JOHNSON,LYNDON
      25          35             KENNEDY,JOHN
      26          16          LINCOLN,ABRAHAM
      27           4            MADISON,JAMES
      28          25         MCKINLEY,WILLIAM
      29           5             MONROE,JAMES
      30          37            NIXON,RICHARD
      31          44             OBAMA,BARACK
      32          14          PIERCE,FRANKLIN
      33          11               POLK,JAMES
      34          40            REAGAN,RONALD
      35          32       ROOSEVELT,FRANKLIN
      36          26       ROOSEVELT,THEODORE
      37          27      TAFT,WILLIAM_HOWARD
      38          12           TAYLOR,ZACHARY
      39          33             TRUMAN,HARRY
      40          10               TYLER,JOHN
      41           8         VAN_BUREN,MARTIN
      42           1        WASHINGTON,GEORGE
      43          28           WILSON,WOODROW
start with changing it to int main.
Okay. But can you help me with getting started with the pointer?
when you declare an ifstream or an ofstream it creates a pointer that points to the beginning of the file. so....
Dude, you are no help at all.
ok thanks for that... just work on how you upload the information into an array. then sorting is a GREAT idea.
Topic archived. No new replies allowed.