sorting

have this program that everything works and compiles but in my output i have to sort by term(semester and year) order goes as follows Spring, Summer and fall also earlier years first... so for some reason it will not sort correctly? this is an OOP database program in c++ please help due tomorrow!

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
#include "Course.h"

	#include <cctype>
	#include <cstring>
	#include <iostream>
	using std::cin;
	using std::cout;
	using std::endl;
	using std::ios;

	#include <iomanip>
	using std::setprecision;
	using std::setw;


	int myStricmp(const char* dst, const char* src)
	{
		int f, l;
		do
		{
			if ((tolower(f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) f -= 'A' - 'a';
			if ((tolower(l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) l -= 'A' - 'a';
		}
		
		while (tolower(f && (f == l)));
		return(f - l);
	}

	int myStrnicmp(const char* dst, const char* src, int count)
	{
		int f, l;
		do
		{
			if ((tolower(f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) f -= 'A' - 'a';
			if ((tolower(l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) l -= 'A' - 'a';
		}
		while (--count && f && (f == l));
		return (f - l);
	}	


	void Course::setName(char enteredName[])
	{
  		strcpy(name, enteredName);
	}

	void Course::setTerm(char enteredTerm[])
	{
 		strcpy(term, enteredTerm);
	}
	void Course::setUnits(int enteredUnits)
	{
  		units = enteredUnits;
	}
	void Course::setGrade(char enteredGrade)
	{
  		grade = enteredGrade;
	}

	void Course::printCourse() const
	{
 	 	cout.setf(ios::left, ios::adjustfield);

  		cout << setw(12) << name << setw(10) << term << setw(7) << units << grade;
  		cout << endl;
	}

	bool Course::compareNames(Course a, Course b) 
	{
  		bool result = false;
  		if ( strcmp( a.name , b.name ) < 0 ) result = true;
  		return result;
	}

	bool Course::compareTerms(Course a, Course b) 
	{
  		bool result = false;
  		if ( strcmp( a.term , b.term ) > 0) result = true;
  		return result;
	}

	bool Course::compareUnits(Course a, Course b) 
	{
  		bool result = false;
  		if ( a.units > b.units ) result = true;
  		return result;
	}

	bool Course::compareGrades(Course a, Course b)
	{
		bool result = false;
  		if ( tolower(a.grade) < tolower(b.grade) ) result = true;
  		return result;
	}   

	bool Course::CourseCmp ( const Course a, const Course b)
	{
 		 // validate string length
 		if ( ( strlen( a.term ) != 6) || ( strlen( b.term ) != 6 ) )
  		return (myStricmp( a.name, b.name ));
    

  		// handle ties
  		if ( myStricmp( a.term, b.term ) == 0 )
 		return ( myStricmp( a.name, b.name ));
     
  		// compare the years
  		int yearA = atoi( a.term + 2);
  		int yearB = atoi (b.term + 2);
  		if( yearA < yearB )
    	return true; // termA comes first
  		if( yearA > yearB )
    	return false; // termB comes first

  		// compare semesters in case of same year
  		if( myStrnicmp( a.term, "SP", 2 ) == 0 )
    		return true;
  		if(myStrnicmp( a.term, "SU", 2 ) == 0 )
    		return myStrnicmp( b.term, "SP", 2 ) ? true : false;
  			return false; 
	}    



How does "term" looks like?
term??
rest of program
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
	#ifndef COURSE_H 
	#define COURSE_H

	class Course
	{
		private: 
  		char name[11];
  		char term[7];
 		int units;
  		char grade;

		public: 
  		void setName(char[]);
  		void setTerm(char[]);
  		void setUnits(int);
  		void setGrade(char);

  		void printCourse() const;
  		static bool compareNames(Course, Course) ;
  		static bool compareTerms(Course, Course) ;
  		static bool compareUnits(Course, Course) ;
  		static bool compareGrades(Course, Course) ;
  		static bool CourseCmp(Course, Course) ;


	};
	#endif

#include "Course.h"

	#include <list>
	using std::list;

	#include <iostream>
	using std::cin;
	using std::cout;
	using std::endl;
	using std::ios;

	#include <iomanip>
	using std::setprecision;
	using std::setw;

	#include <fstream>
	using std::fstream;
	
	#include <sstream>
	using std::ostringstream;

	#include <vector>
	#include <cstdlib> 

	// function prototypes
	void printClasses(list<Course>&);
	void saveList(list<Course>&);
	void restoreList(list<Course>&);


	int main()
	{
 
  		
  		list<Course> courseList;

		// prompt user if they would like to load saved course list
  		while (1)
  		{
			cout << "Would you like to load your saved course data??? \n";
			cout << "[Yes = Y || No = Blank list for entry]: ";

    		char userInput = 0;
   			cin >> userInput;
    		cin.ignore(1000,10);
    		cout << endl;

    		if (userInput == 'Y' || userInput == 'y')
    		{
      			restoreList(courseList);
				break;	
    		}
    		
    		else if (userInput == 'N' || userInput == 'n' )
    		{ 
      			break;
    		}
   
   	 		else
    		{
      			cout << "Invalid input please press enter to continue.... \n";
      			cin.get();
    		}


  		}

		// whichever selection user makes prompt user to add data
		while (1)
  		{
  			cout << endl;
  			cout << "Would you like to add course data to your list?? \n";
  			cout << "[Y = input new info || N = Print courses/Quit: ";
  			
    		char userInput = 0;
    		cin >> userInput;
    		cin.ignore(1000,10);

			// if user selects yes initiate course add
    		if (userInput == 'Y' || userInput == 'y')
    		{
      			Course courseToAdd;

				cout << endl;
				cout << "Enter course information in the following order seperated by spaces... \n";
				cout << endl;
				cout << "COURSE NAME [EX: comsc165]  TERM [EX: FA2013]  UNITS [EX: 4]  GRADE [EX: A-F] \n";
      			
      			char course[11];
      			cin >> course;
      			courseToAdd.setName(course);

      			char term[7];
      			cin >> term;
      			courseToAdd.setTerm(term);

				// make sure unit entered is a number
      			char unitBufferA[32];
      			char unitBufferB[2] = {0};
				cin >> unitBufferA;
      			unitBufferB[0] = unitBufferA[0];
				courseToAdd.setUnits( atoi(unitBufferB));

      			char grade;
      			cin >> grade;
      			courseToAdd.setGrade(grade);
				cin.ignore(1000, 10);

			    courseList.push_back(courseToAdd);

    		}
        
       	 	// if user selects no print list to console
        	else if (userInput == 'N' || userInput == 'n')
    		{
     			cout << endl;
     			cout << "	COURSE SCHEDULE \n";
     			cout << "	--------------- \n";
      			printClasses(courseList);
				break;
    		}
    
    		else 
    		{
    			cout << endl;
    			cout << "Invalid entry press any key to re-enter [Y/N]: ";
    			cin.get();
    		}

  		} // end of user input save list
  		saveList(courseList);

		// print list to console
  		cout << "	COURSES SORTED BY NAME \n ";
  		cout << "	---------------------- \n";
  		courseList.sort(Course::compareNames); 
  		printClasses(courseList);

  		cout << "	COURSES SORTED BY TERM \n";
  		cout << "	---------------------- \n";
  		courseList.sort(Course::compareTerms); 
  		printClasses(courseList);

		cout << "	COURSES SORTED BY GRADE \n ";
		cout << "	----------------------- \n";
  		courseList.sort(Course::compareGrades); 
  		printClasses(courseList);
  		
  		cout << "	COURSES SORTED BY UNIT \n ";
  		cout << "	---------------------- \n";
  		courseList.sort(Course::compareUnits); 
  		printClasses(courseList);	

	} // end of main 

	// Functions
	void printClasses(list<Course>& courseList)
	{
  		cout.setf(ios::left, ios::adjustfield);
		cout << setw(12) << "COURSE" << setw(8) << "TERM" << setw(7) << "UNITS" << "GRADE" << endl;
		cout << setw(12) << "----------" << setw(8) << "------" << setw(7) << "-----" << "-----" << endl;

  		for (list<Course>::iterator p = courseList.begin(); p != courseList.end(); p++)
  		{
    		p->printCourse();
  		}
  			cout << setw(12) << "----------" << setw(8) << "------" << setw(7) << "-----" << "-----" << endl << endl;
		}

	// restore list 
	void restoreList(list<Course>& courseList)
	{
 		fstream fin; 
  		fin.open("Courses.dat", ios::binary|ios::in); 
  		if (!fin) 
    	return;

  		int records; 
  		fin.read((char*)&records, sizeof(int));

  		for (int i = 0; i < records; i++) 
  		{ 
    		Course courseToAdd;
    		fin.read((char*)&courseToAdd, sizeof(Course));
    		courseList.push_back(courseToAdd);
		} 
  			fin.close(); 
	}

	// save the list
	void saveList(list<Course>& courseList)
	{
  		fstream saveList; 
  		saveList.open("Courses.dat", ios::binary|ios::out); 
  		
  		int records = courseList.size();
  
 		saveList.write((char*)&records, sizeof(int)); 

  		for (list<Course>::iterator x = courseList.begin(); x != courseList.end(); x++)
  		{
    		Course c = *x;
    		saveList.write((char*)&c, sizeof(Course));
  		}
   			saveList.close(); 
	}
TERM [EX: FA2013]
So if you have following terms, you want to sort them like that?
1
2
3
4
FA2013
SP2013
SP2012
FA2012
FA2012
FA2013
SP2012
SP2013
Last edited on
no per year sorted spring, summer, fall and if say you had even lower case input to and also say su2012 and fa 2012 id want it to be

1.SP2012
2.su2012
3.FA2012 [fa and FA doesnt matter what order if the same year]
4. fa2012
5.SP2013
6.FA2013
this is some input i got from console after entering in some data

COURSES SORTED BY TERM
----------------------
COURSE TERM UNITS GRADE
---------- ------ ----- -----
comsc210 su2012 5 b
math3 sp2013 3 c
math210 sp2012 4 d
eng122 sp2010 5 a
comsc110 fa2012 4 a
comsc 156 0 5
---------- ------ ----- -----
for some reason puts summer first and 2010 is in the middle there for some reason
return myStrnicmp( b.term, "SP", 2 ) ? true : false;
REmember that o is false, so if strings are equal it will return false.
isnt that what i have?
still giving wacky results putting 2010 in the middle still? i cant figure it out?!
Your function does not handles "FA" semester.
programs due in like an hour really cant even think right now anyway you can just show me what you mean help me out? been working on this program all night and i think thats really the only thing wrong?
Topic archived. No new replies allowed.