sorting string members in an array of 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;
	}
}
i am having trouble with the sortName function as it is not sorting the strings.

You want to sort an array of objects based on one aspect (in this case the name) of those objects. You do not want to sort strings.
As I'm reading your coding, it is a bit confusing because you are using variable names that are keywords which is not suggested to do. Swap is a function on its own and shouldn't be used as a variable name, array as well. It makes your code unclear on what your actual intent is for the program rather it is suppose to be used or it's just a variable name. Because we have so many functions that are wanting to use the Struct. It is best to use Classes and make them public (Not suggested) and then you can add in codes for the classes and calling them is much easier. You'll find it easier to work with classes for something like this. Structs. are used to keep data members mostly. But if you need to set and get them then a class is the way to go.

Another thing for struct and classes is you want to identify them a bit easier such as m_variable name. So that away a programmer knows they are working with a data member. I keep having to scroll up and down to see what variable I'm working with and how it's being called and set. I would suggest re-writing the code since you're working with structs. just know that they are like classes expect classes by default are private and structs. by default is public. Make yourself a Header file for your struct. if you plan on still using it. It makes reading your code much easier.

Because we have so many functions that are wanting to use the Struct. It is best to use Classes

The use of struct and class are practically identical.

Another thing for struct and classes is you want to identify them a bit easier such as m_variable name. So that away a programmer knows they are working with a data member. I keep having to scroll up and down to see what variable I'm working with and how it's being called and set.

I don't know why you would need to "scroll" here. If it is preceded by a . it's a member.

Topic archived. No new replies allowed.