Sorting Function Almost Sorting

Hello,

I'm trying to write a program that will for the time being, display the finishing order of a race, given the "results" file of that race from the server it was run on.

Thus far I've been able to get the information from the file I want, and get it into a string vector (first program output). This string vector is then passed to my sorting function (code). The sorting function the returns another string vector (second program output).

So as you can see, it nearly gets it right. The only problem is with "Jay Shadd". All I can come up with is that his race time is shorter when compared to "BT Dibble", even though his lap count is shorter (meaning his race was shorter even though he is a lap behind Dibble, which is possible). The thing is, when I look at my sorting function, I can't figure out why this is a problem. The first thing the sorting function does is compare the number of laps from each driver, and since this situation makes the if statement true, it should switch the information around. I can't figure out why it isn't doing this though. Any insight you could provide would be much appreciated.

Thanks very much!
Jeff

String vector given to sorting function:

Driver
Glenn Briden
Laps
0
RaceTime
DNF
Driver
Jeff Cable
Laps
5
RaceTime
DNF
Driver
Davy Benham
Laps
6
RaceTime
DNF
Driver
Thomas Benham
Laps
16
RaceTime
DNF
Driver
Scott Newgent
Laps
19
RaceTime
0:30:54.535
Driver
Racker2
Laps
19
RaceTime
0:30:38.997
Driver
BT Dibble_H
Laps
18
RaceTime
0:31:35.802
Driver
Cesare
Laps
19
RaceTime
0:30:47.260
Driver
Billy Livingston
Laps
19
RaceTime
0:30:28.057
Driver
Greg7
Laps
17
RaceTime
0:31:18.978
Driver
Eggs
Laps
19
RaceTime
0:30:57.357
Driver
Jay Shadd
Laps
17
RaceTime
0:30:35.298


Sorting Function:

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
vector<string> sorter(vector<string> a) {

	int i = 0;
	int s = a.size();
	while(i < s)
	{
		int l = a.size();
		int c = 0;
		while(c < (l-6))
		{
			int D1T;
			int D2T;

			std::string D1L = a[c+3];					
			double L1 = ::atof(D1L.c_str());		//converts both driver's lap count strings to numbers
			std::string D2L = a[c+9];
			double L2 = ::atof(D2L.c_str());


			vector<string> racetd1;

			char * writable1 = new char[a[c+5].size() + 1];
			std::copy(a[c+5].begin(), a[c+5].end(), writable1);
			writable1[a[c+5].size()] = '\0'; 
			char* pch;
			pch = strtok(writable1, ": ." );		//creates workable time vector for driver 1
			while (pch !=NULL)
			{
				racetd1.push_back(pch);
				pch = strtok (NULL, ": .");
			}

			vector<string> racetd2;
			char * writable = new char[a[c+11].size() + 1];
			std::copy(a[c+11].begin(), a[c+11].end(), writable);
			writable[a[c+11].size()] = '\0'; 
			char* pch1;
			pch1 = strtok(writable, ": ." );		//creates workable time vector for driver 2
			while (pch1 !=NULL)
			{
				racetd2.push_back(pch1);
				pch1 = strtok (NULL, ": .");
			}



			if (racetd1.size() > 2) 
			{
				std::string htd1 = racetd1[0];
				int hfd1 = ::atof(htd1.c_str());
				std::string mtd1 = racetd1[1];
				int mfd1 = ::atof(mtd1.c_str());
				std::string std1 = racetd1[2];		//creates total time in ms for driver 1
				int sfd1 = ::atof(std1.c_str());
				std::string tstd1 = racetd1[3];
				int tsfd1 = ::atof(tstd1.c_str());
				D1T = tsfd1 + (sfd1 * 1000) + (mfd1 * 60000) + (hfd1 * 3600000);
			}
			else 
			{
				D1T = 86400000;
			}

			if (racetd2.size() > 2) 
			{
				std::string htd2 = racetd2[0];
				int hfd2 = ::atof(htd2.c_str());
				std::string mtd2 = racetd2[1];
				int mfd2 = ::atof(mtd2.c_str());
				std::string std2 = racetd2[2];		//creates total time in ms for driver 2
				int sfd2 = ::atof(std2.c_str());
				std::string tstd2 = racetd2[3];
				int tsfd2 = ::atof(tstd2.c_str());
				D2T = tsfd2 + (sfd2 * 1000) + (mfd2 * 60000) + (hfd2 * 3600000);
			}
			else 
			{
				D2T = 86400000;
			}


			if ( L1 < L2 )							//checks if second driver in list has completed more laps,
			{										//if they have, switch their information with the first driver
				string t0 = a[c];
				string t1 = a[c+1];
				string t2 = a[c+2];
				string t3 = a[c+3];					//creates temporary copies of driver 1's information
				string t4 = a[c+4];
				string t5 = a[c+5];

				a[c] = a[c+6];
				a[c+1] = a[c+7];
				a[c+2] = a[c+8];
				a[c+3] = a[c+9];					//swaps driver 1's info for driver 2's
				a[c+4] = a[c+10];
				a[c+5] = a[c+11];

				a[c+6] = t0;
				a[c+7] = t1;
				a[c+8] = t2;
				a[c+9] = t3;						//swaps driver 2's info for driver 1's
				a[c+10] = t4;
				a[c+11] = t5;

			}
			else
			{
				if ( L1 = L2 )						//checks if the number of laps are the same
				{									//if laps are the same, compares total time
					if( D1T > D2T )					//if the second driver's time is less than the first,
					{								//exchange the information
						string t0 = a[c];
						string t1 = a[c+1];
						string t2 = a[c+2];
						string t3 = a[c+3];			//creates temporary copies of driver 1's information
						string t4 = a[c+4];
						string t5 = a[c+5];

						a[c] = a[c+6];
						a[c+1] = a[c+7];
						a[c+2] = a[c+8];
						a[c+3] = a[c+9];			//swaps driver 1's info for driver 2's
						a[c+4] = a[c+10];
						a[c+5] = a[c+11];

						a[c+6] = t0;
						a[c+7] = t1;
						a[c+8] = t2;
						a[c+9] = t3;				//swaps driver 2's info for driver 1's
						a[c+10] = t4;
						a[c+11] = t5;

					}
				}
				//Note: I'm not checking to see if L1 > L2 or if D2T > D1T because that would not require any transfer of information,
				//and by checking to the if the other conditions are true, anything that fails all of the if statements, should fall under
				//this category.
			}
			c = c + 6; //sets the inner while loop to compare driver 2 with the next driver in the list on next run-through
		}
		s = s - 1; //progresses the outer while loop
	}
	return a; //return the sorted list
}


Sorting function returns this:

Driver
Billy Livingston
Laps
19
RaceTime
0:30:28.057
Driver
Racker2
Laps
19
RaceTime
0:30:38.997
Driver
Cesare
Laps
19
RaceTime
0:30:47.260
Driver
Scott Newgent
Laps
19
RaceTime
0:30:54.535
Driver
Eggs
Laps
19
RaceTime
0:30:57.357
Driver
Jay Shadd
Laps
17
RaceTime
0:30:35.298
Driver
BT Dibble_H
Laps
18
RaceTime
0:31:35.802
Driver
Greg7
Laps
17
RaceTime
0:31:18.978
Driver
Thomas Benham
Laps
16
RaceTime
DNF
Driver
Davy Benham
Laps
6
RaceTime
DNF
Driver
Jeff Cable
Laps
5
RaceTime
DNF
Driver
Glenn Briden
Laps
0
RaceTime
DNF
Topic archived. No new replies allowed.