For the love of god, help me!!!!

Alright, I'm a total n00b to C++ (taking it in school right now). So I've been working on my homework quite a bit.. even going as far as to add things that we haven't learned yet (yeah, I'm an over-achiever.) but maybe I'm not getting the things that I've glanced over. This program is supposed to take three runner's names and track times. I've sorted the track time into arrays and I did the math on the milliseconds so it's easier for the computer to compare who has the fastest time. They're supposed to be displayed in first, second, and third. I've desk-checked my program, like three times, and I've changed things over and over trying to get this thing to sort all the way. I've even did the math in my head to make sure the math was right. Please someone help me. It'll probably be too late by the time I get an answer, but striving to be a good programmer, I need to know my mistake! Oh, and make it simple.. like starting out C++ basic coding, lol!

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
 /* 
This program will take the names 
and times of three runners and display
them by the fastest time.

Last Modified 09.19.2013 
*/
#include <iostream>
#include <string>
using namespace std;

//This is the main module.
int main()
{
	//Declare the variables.
	const int SIZE = 3;
	string names[SIZE];
	int minutes[SIZE],
		seconds[SIZE],
		milliseconds[SIZE],
		flag = 1;
	unsigned long order[SIZE];
	
	//Display a welcome message.
	cout << " \n";
	cout << "This program will take the name of three runners and their times and\n";
	cout << "display the results by the fastest times in first, second, and third\n";
	cout << "place.\n";
	cout << " \n";
	cout << "Last Modified 09.19.2013\n";
	
	//Ask the user for the three runner's names and track times.
	//Create a counter variable.
	int index = 0;
	
	//Get the names and track times.
	for (index = 0; index <= SIZE-1; index++)
	{
		cout<< " \n";
		cout << "Enter the name of runner # " << index+1 << ".\n";
		cout << " \n";
		
		getline(cin, names[index]);
						
		//Set a message about input to display only once.
		while (flag)
		{	cout << " \n";
			cout << "The next three inputs will be the MINUTES, SECONDS, and\n";
			cout << "MILLISECONDS of the total track time.\n";
			cout << " \n";
			flag = 0;
		}
			
		
		//Get the MINUTES, SECONDS, and MILLISECONDS of the track time.
		do
		{	cout << " \n";
			cout << "Enter runner number " << index + 1 << "'s track time MINUTES.\n";
			cout << "Only numbers are valid.\n";
			cin >> minutes[index];
		}while (minutes[index] < 0);
		
		order[index] = order[index] +(600 * minutes[index]);
		
		do
		{	cout << " \n";
			cout << "Enter runner number " << index + 1 << "'s track time SECONDS.\n";
			cout << "Only numbers are valid.\n";
			cin >> seconds[index];
		}while (seconds[index] < 0 || seconds[index] >= 60);
		
		order[index] = order[index] + (seconds[index] * 10);
	
		do
		{	cout << " \n";
			cout << "Enter runner number " << index + 1 << "'s track time MILLISECONDS.\n";
			cout << "Only numbers are valid.\n";
			cin >> milliseconds[index];
			cin.ignore();
		}while (milliseconds[index] < 0 || milliseconds[index] >= 100);

		order[index] = order[index] + milliseconds[index];
	}	
	
	//Sort the first, second, and third place runners by fastest time.
	//Declare variable to hold last element of array.
	
	for (int maxElement = 1; maxElement < SIZE; maxElement++)
	{
		for (index = 0; index < SIZE - 1; index++)
		{
			if (order[index] > order[index+1])
			{
				//Swap current and next elements in arrays.
				//Declare temp variables.
				int minTemp = 0,
					secTemp = 0,
					millTemp = 0,
					orderTemp = 0;
				string runTemp = "";
			
				//switch variables with their next element in the array.
				minTemp = minutes[index];
				minutes[index] = minutes[index+1];
				minutes[index+1] = minTemp;
			
				secTemp = seconds[index];
				seconds[index] = seconds[index+1];
				seconds[index+1] = secTemp;
			
				millTemp = milliseconds[index];
				milliseconds[index] = milliseconds[index+1];
				milliseconds[index+1] = millTemp;
			
				runTemp = names[index];
				names[index] = names[index+1];
				names[index+1]= runTemp;
			
				orderTemp = order[index];
				order[index] = order[index+1];
				order[index+1] = orderTemp;	
			}
		}
	}
	
	//Display the results.
	
	for (index = 0; index <= SIZE - 1; index++)
	{	cout << " \n";
		cout << index + 1 << " Place : " << names[index] << endl;
		cout << "TIME : " << minutes[index] << ":" << seconds[index] << "." << milliseconds[index] << endl;
	}
	
	return 0;
}
Last edited on
order[index] = order[index] + (600 * minutes[index]);:
You are adding the value in order[index](which is not set) to something causing it to store some trash data


Last edited on
Sorry, could you elaborate? Here's my logic on it to help you better understand the errors of my ways...

If someone ran a track in 13:10.50, I have an array set for each : minutes, seconds, and milliseconds. What I did (or attempted to do) was this:
minutes - 13 min * 600ms = 7800ms
seconds - 10 sec * 10ms = 100ms
milliseconds - 50ms
so 7800 + 100 + 50 = 7900ms total.

So, if I convert each runner's track time into total milliseconds, the computer can calculate easily who has the fastest time through bubblesort (or just any sort that will work by comparing the order[array].)

So, could you explain to me what some solutions might be without me having to rewrite the whole thing?
nevermore28, I want to kiss your face!!!! after reading over what you said a few times, I realized what you meant and fixed it by adding order[index] = 0 to my loop so it initialized that spot with an integer!!!! AND IT FIXED IT! I LOVE YOU! You're so great. I bet you look at yourself everyday and wonder how someone so amazing can be on this earth.

You. are. amazing.

Thanks.
Topic archived. No new replies allowed.