sorting certain string member in an array struct

i am having trouble with the sortName function as it is not sorting the strings.I am weak on string sorting, havent done much work with it. Not sure what i am doing wrong. Any feedback will help me greatly thanks!

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <string>
using namespace std;

struct stuInfo
				{	string name;
					int idNum;
					float cExamGrade,
						  pExamGrade,
						  overallGPA;
				};

void sortName(stuInfo array[],int size);
void sortID(stuInfo array[],int size );
void sortcExam(stuInfo array[],int size);
void sortpExam(stuInfo array[],int size);
void sortOverallGPA(stuInfo array[],int size);

int main(){
	
	int arraySize,
		choice;
		
	cout<<"How many students are there?: ";
	cin>>arraySize;
	
	stuInfo info[arraySize];
	
	cout<<"Enter the information specified.\n"<<endl;
	
	for(int i=0 ;i<arraySize;i++){
		
		cin.ignore();
		cout<<"\nName for student "<<i+1<<": ";
		getline(cin,info[i].name);
		
		cout<<"ID number: ";
		cin>>info[i].idNum;
		
		cout<<"Current exam grade: ";
		cin>>info[i].cExamGrade;
		
		cout<<"Prior exam grade: ";
		cin>>info[i].pExamGrade;
		
		cout<<"Overall GPA: ";
		cin>>info[i].overallGPA;
	}
	
	cout<<"\nSelect what you would like to sort\n"
		<<"----------------------------------\n"
		<<"1 for name\n"
		<<"2 for ID number\n"
		<<"3 for current exam grade\n"
		<<"4 for prior exam grade\n"
		<<"5 for overall GPA\n"
		<<"6 for no sort\n\n"
		<<"Enter: ";
		
	cin>>choice;
	
	switch(choice)
	{
		case 1 :
			sortName(info,arraySize);
			break;
		case 2 :
			sortID(info,arraySize);
			break;
		case 3:
			sortcExam(info,arraySize);
			break;
		case 4:
			sortpExam(info,arraySize);
			break;
		case 5:
			sortOverallGPA(info, arraySize);
			break;
		case 6:
			cout<<"EXITED";
			return 0;
			break;
		default:
			while(choice<1 && choice>6)
			{
				cout<<"Enter valid choice: ";
				cin>>choice;
			}
			
		
	}
	
}
void sortName(stuInfo array[],int size )
{
	bool swap;
	string temp;
	
	do
	{
		swap = false;
		for(int i= 0; i<(size-1);i++)
		{	
			if (array[i].name>array[i+1].name)
			{
				temp=array[i].name;
				array[i].name=array[i+1].name;
				array[i+1].name=temp;
				swap=true;
			}
		}
	}while(swap);
	
	for(int i=0;i<size;i++)
	{
		cout<<array[i].name<<endl;
	}
}
void sortID(stuInfo array[],int size )
{
	bool swap;
	float temp;
	
	do
	{
		swap = false;
		for(int i= 0; i<(size-1);i++)
		{	
			if (array[i].idNum>array[i+1].idNum)
			{
				temp=array[i].idNum;
				array[i].idNum=array[i+1].idNum;
				array[i+1].idNum=temp;
				swap=true;
			}
		}
	}while(swap);
	
	for(int i=0 ; i<size; i++)
	{
		cout<<array[i].idNum<<endl;
	}
}
void sortcExam(stuInfo array[],int size )
{
		bool swap;
	float temp;
	
	do
	{
		swap = false;
		for(int i= 0; i<(size-1);i++)
		{	
			if (array[i].cExamGrade>array[i+1].cExamGrade)
			{
				temp=array[i].cExamGrade;
				array[i].cExamGrade=array[i+1].cExamGrade;
				array[i+1].cExamGrade=temp;
				swap=true;
			}
		}
	}while(swap);
	
	for(int i=0 ; i<size; i++)
	{
		cout<<array[i].cExamGrade<<endl;
	}
}
void sortpExam(stuInfo array[],int size )
{
		bool swap;
	float temp;
	
	do
	{
		swap = false;
		for(int i= 0; i<(size-1);i++)
		{	
			if (array[i].pExamGrade>array[i+1].pExamGrade)
			{
				temp=array[i].pExamGrade;
				array[i].pExamGrade=array[i+1].pExamGrade;
				array[i+1].pExamGrade=temp;
				swap=true;
			}
		}
	}while(swap);
	
	for(int i=0 ; i<size; i++)
	{
		cout<<array[i].pExamGrade<<endl;
	}
}
void sortOverallGPA(stuInfo array[],int size )
{
		bool swap;
	float temp;
	
	do
	{
		swap = false;
		for(int i= 0; i<(size-1);i++)
		{	
			if (array[i].overallGPA>array[i+1].overallGPA)
			{
				temp=array[i].overallGPA;
				array[i].overallGPA=array[i+1].overallGPA;
				array[i+1].overallGPA=temp;
				swap=true;
			}
		}
	}while(swap);
	
	for(int i=0 ; i<size; i++)
	{
		cout<<array[i].overallGPA<<endl;
	}
}
Last edited on
You can't declare static arrays like that.
1
2
3
4
cout<<"How many students are there?: ";
cin>>arraySize;

stuInfo info[arraySize];


http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/dynamic/
Topic archived. No new replies allowed.