undeclared temp

I am not really sure what type to declare "temp". Here's my code.

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

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

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

  while(!fin.eof())
  {
    plist[n].chronum = n+1;
    n++;
    fin >> plist[n].pname;
  }
  
  for(end=n-1; end>0; end--)
  {
    maxindex = 0;
    for(i=1; i<=end; i++)
    {
      if(strcmp(plist[i].pname, plist[maxindex].pname)>0)
        maxindex=i;
      temp = plist[maxindex];
      plist[maxindex]=plist[end];
      plist[end]=temp;
    }
  }
  
  cout << i << plist[i].chronum << setw(30) << left << plist[i].pname << plist[i].next << endl;
}
You shouldn't declare temp at all. Use std::swap (and while you're at it, std::sort).
Last edited on
or,, you can do like
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
#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;
  fin.open("prez.dat");
  fin >> plist[n].pname;

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


edit:added code tag,fixed [b] tag
Last edited on
Topic archived. No new replies allowed.