How to sort structure arrays

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
typedef struct record1{
int n1;
string name1;
char c1;
double d1;
} set1;

int swap2(int &n1, int &n2){
int temp;
temp=n1;
n1=n2;
n2=temp;

return 1; } // SWAP 2 NUM

int swap2(char &n1, char &n2){
char temp;
temp = n1;
n1 = n2;
n1 = temp;

return 1; } // SWAP 2 CHAR

int swap2(double &n1, double &n2){
double temp;
temp = n1;
n1 = n2;
n2 = temp;

return 1; } // SWAP 2 DOUBLE

int swap2(char n1[20], char n2[20]){
char temp[20];
strcpy(temp[20],n1[20]);
strcpy(n1[20],n2[20]);
strcpy(n2[20],temp[20]);

return 1; } // SWAP 2 STRING


int bubble1(set1 *p){
int i,j,temp,flag;
for(j=0; j<90; j++){
flag=0;
for(i=0; i<89; i++){
if(((p+i)->n1) > ((p+i+1)->n1)){
swap2(((p+i)->n1), ((p+i+1)->n1));
swap2(((p+i)->c1), ((p+i+1)->c1));
swap2(((p+i)->d1), ((p+i+1)->d1));
swap2(((p+i)->name1), ((p+i+1)->name1));
flag =1;
} // IF
} // SORT
if(flag==0)break;
} // J

return 0;
}


int main(){
clrscr();
int ct;
ifstream in1("names.dat");
set1 *s1;
s1 = new set1[100];



for(ct=0;!in1.eof();++ct){
in1>>(s1+ct)->n1;
if(((s1+ct)->n1) == -1)break;
in1>>(s1+ct)->name1;
in1>>(s1+ct)->c1;
in1>>(s1+ct)->d1;
} // FOR



for(int i=0; i<ct; i++){
cout << "\n";
cout << "\t" << (s1+i)->n1;
cout << "\t" << (s1+i)->d1;
cout << "\t\t" << (s1+i)->c1;
cout << "\t" << (s1+i)->name1;
if((i%15==0)&&(i>0)){
cout << "\n\t";
cout << "\n\tEnter to continue...";
getch();
}
} // FOR
cout << "\n\n\t Bubble Sort in Progress...\n";

for(int i=0; i<ct; i++){
bubble1(s1);
cout << "\n";
cout << "\t" << (s1+i)->n1;
cout << "\t" << (s1+i)->d1;
cout << "\t\t" << (s1+i)->c1;
cout << "\t" << (s1+i)->name1;
if((i%15==0)&&(i>0)){
cout << "\n\t";
cout << "\n\tEnter to continue...";
getch();
}
} // FOR

cout << "\n\tTotal =\t" << ct;

delete []s1;
in1.close();
cout << "\n\tComplete";


return 0;
} //MAIN 


i need to get this program to sort the numbers and print them out, the string function doesn't work so i don't think it sorts everything... help please!!

i know i'm not really supposed to post the whole source code, but i think it may be a problem in the way i'm writing it. the numbers or n1 sort, but nothing else does.

6 ACTON Y 185.000
198 CONSTANCE Q 42.182

thats a few things from the names.dat file so you could get familiar with it. thanks!
Topic archived. No new replies allowed.