Scheduling Program

Just a little background... I started learning C++ two years ago and I stopped coding about a year ago. I am picking it back up again and decided to create a scheduling program to refresh my memory. This program's intentions are for college students to be able to input a series of different class schedules and the program will then find solutions of schedules that have no time overlapping and display these to the user... So far I have coded the prompting up to what days a week each class is, and now it is time for the "time". (By the way I have all the information stored in a 3 dimensional array so far, class, option #, day.) Any ideas on how to receive frames of time from the user to put into the fourth dimension of the array that will later be able to be compared to each other? I'm assuming it is going to have to by in military time. I pasted my code below for where I am so far... (Tell me if I am a little over my head, I chose this program to script because I would actually have a practical use for it, as with my friends.)

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
// Program Name: Scheduler.cpp
// Author: *************
// Date: 6/27/2012
// Description: This program will give you all the possibilities of a schedule given class options.

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

string class_name[4];
int class_options[4], class_amount, loop = 1, class_day_amount[4][4];
char class_days[4][4][4];

int main()
{


	cout << "****************************************" << endl
	     << "* Scheduler v1.0 ---------------------- *" << endl
		 << "****************************************" << endl << endl;
	cout << "This program will give you all possibilities of a schedule." << endl << endl;
	cout << "Number of classes being taken: ";
	cin >> class_amount; // Amount of classes being taken.
	while(loop == 1) // Checks for incorrect input.
		{
			if(class_amount > 5 || class_amount < 2)
				{
						cout << "The amount you entered was not operable, choose from 2 to 5 classes, try again: " << endl;
						cin >> class_amount;
				}
			else 
				{
					loop = 0;
				}

		}

	loop = 1; // Resets while loop.

	for(int m = 0; m < class_amount; m++) // Retrieves the names of all the classes.
		{
			cout << "Name of class #" << m + 1 << ": ";
			cin >> class_name[m];
		}

	//cout << endl << "The " << class_amount <<  " classes you are taking this semester are: " << endl;

	//for(int m = 0; m < class_amount; m++)
	//	{
	//		cout << "#" << m + 1 << ": " << class_name[m] << endl;
	//	}

	cout << endl;

	for(int m = 0; m < class_amount; m++) // Retrieves the number of options for each class.
		{
			cout << "Number of different options for class #" << m + 1 << " (" << class_name[m] << "): ";
			cin >> class_options[m];
			while(loop == 1) // Checks for incorrect input.
				{
					if(class_options[m] < 0)
						{
								cout << "The amount you entered was not operable, try again: ";
								cin >> class_options[m];
						}
					else 
						{
							loop = 0;
						}

				}
		}

	loop = 1; // Resets while loop.

	for(int x = 0; x < class_amount; x++) // Retrieves number of days per week for each class.
		{
			cout << endl << "Class #" << x + 1 << " has " << class_options[x] << " options." << endl;

			for(int y = 0; y < class_options[x]; y++)
				{
					cout << "How many days a week does class #" << x + 1 << " (" << class_name[x] << ") Option #" << y + 1 << " have: ";
					cin >> class_day_amount[x][y];
					while(loop == 1) // Checks for incorrect input.
						{
							if(class_day_amount[x][y] < 1)
								{
										cout << "The amount you entered was not operable, try again: ";
										cin >> class_day_amount[x][y];
								}
							else 
								{
									loop = 0;
								}

						}
				}
		}

	loop = 1; // Resets while loop.

	cout << endl << "**************" << endl
		 << "*   Legend   *" << endl
		 << "**************" << endl
		 << "*[M]onday    *" << endl
		 << "*[T]uesday   *" << endl
		 << "*[W]ednesday *" << endl
		 << "*Th[u]rsday  *" << endl
		 << "*[F]riday    *" << endl
		 << "**************" << endl;

	for(int x = 0; x < class_amount; x++) // Retrieves the specific days per week for each class.
		{
			cout << endl << "Prompting for information on class #" << x + 1 << " (" << class_name[x] << ").";

			for(int y = 0; y < class_options[x]; y++)
				{
					cout << endl << "Prompting for information on Option #" << y + 1 << " of the above class.";

					for(int z = 0; z < class_day_amount[x][y]; z++)
						{
							cout << endl << "Type the hot key for Day #" << z + 1 << " and press <Enter>: ";
							cin >> class_days[x][y][z];
							while(loop == 1) // Checks for incorrect input.
								{
									switch(class_days[x][y][z])
										{
											case 'M':
												loop = 0;
												break;
											case 'm':
												loop = 0;
												break;
											case 'T':
												loop = 0;
												break;
											case 't':
												loop = 0;
												break;
											case 'W':
												loop = 0;
												break;
											case 'w':
												loop = 0;
												break;
											case 'U':
												loop = 0;
												break;
											case 'u':
												loop = 0;
												break;
											case 'F':
												loop = 0;
												break;
											case 'f':
												loop = 0;
												break;
											default:
												cout << "The character you entered was not operable, try again: ";
												cin >> class_days[x][y][z];
												break;
										}

								}
						}
				}
		}

	loop = 1; // Resets while loop.

	for(int x = 0; x < class_amount; x++) // Retrieves the specific times for each day per week for each class.
		{
			cout << endl << "Prompting for information on class #" << x + 1 << " (" << class_name[x] << ").";

			for(int y = 0; y < class_options[x]; y++)
				{
					cout << endl << "Prompting for information on Option #" << y + 1 << " of the above class.";

					for(int z = 0; z < class_day_amount[x][y]; z++)
						{
							cout << endl << "Prompting for information on ";
							switch(class_days[x][y][z])
										{
											case 'M':
												cout << "Monday of the above option and class.";
												break;
											case 'm':
												cout << "Monday of the above option and class.";
												break;
											case 'T':
												cout << "Tuesday of the above option and class.";
												break;
											case 't':
												cout << "Tuesday of the above option and class.";
												break;
											case 'W':
												cout << "Wednesday of the above option and class.";
												break;
											case 'w':
												cout << "Wednesday of the above option and class.";
												break;
											case 'U':
												cout << "Thursday of the above option and class.";
												break;
											case 'u':
												cout << "Thursday of the above option and class.";
												break;
											case 'F':
												cout << "Friday of the above option and class.";
												break;
											case 'f':
												cout << "Friday of the above option and class.";
												break;
											default:
												break;
										}
							for(int v = 0; v < class_days[x][y][z]; v++)
								{
									// Retrieval of time.
								}
						}
				}
		}

	system("pause");
	return 0;
}
Last edited on
Okay, a few things:

1) Indentation. You can of course indent however you like, but in every environment I've worked in, I've never seen anyone indent after a loop like:
1
2
3
for(...)
        {     // <--- unnecessary!
                // <--- good to indent here though, imo 


so instead, I might recommend:

1
2
3
for(...)
{
        cout << ...



2) While loops.
1
2
3
4
5
6
7
8
9
10
11
12
13
while(loop == 1) // Checks for incorrect input.
{
	if(class_options[m] < 0)
	{
		cout << "The amount you entered was not operable, try again: ";
		cin >> class_options[m];
	}
	else 
	{
		loop = 0;
	}

}


could be written more simply as:

1
2
3
4
5
while(class_options[m] < 0)
{
	cout << "The amount you entered was not operable, try again: ";
	cin >> class_options[m];
}


Note that if you really DID need to break out for some abnormal condition, you can do that with a 'break;' statement instead of injecting an additional variable into the loop.

3) Switch statements. You have:
1
2
3
4
5
6
7
8
switch(class_days[x][y][z])
{
case 'M':
	cout << "Monday of the above option and class.";
	break;
case 'm':
	cout << "Monday of the above option and class.";
	break;


This can be simplified one of two ways:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch(class_days[x][y][z])
{
case 'M':
case 'm':
	cout << "Monday of the above option and class.";
	break;


// OR

switch(toupper(class_days[x][y][z]))    // Notice call to toupper
{
case 'M':
	cout << "Monday of the above option and class.";
	break;


Edit: Even better (at least when getting class days), you can just do:
1
2
3
4
5
string days = "MTWUF";
while(days.find(toupper(class_days[x][y][z])) == days.end())
{
    // invalid day, get new one
}




4) Arrays of unknown size (at compile time)
I notice you are getting the 'class amount' from the user, and using that many entries in your array. It might be easier instead to dynamically allocate your arrays based on this value:

1
2
3
4
5
6
7
string * class_name = 0;
cout << "Number of classes being taken: ";
cin >> class_amount; // Amount of classes being taken.

// error checking

class_name = new string[class_amount];


OR, for a more c++ish style, just use
std::vector<string> class_name

5) Time. This is a bit of a tricky one. Many time libraries store specific times as 'the number of seconds since January 1, 1970' (for historical reasons). But in your case, you aren't looking for a single specific time, but a time that will be the same on a weekly basis. There are a variety of ways to implement this. Properly, probably some type of 'Time' class might be used, but for this situation I would probably hack it together as an integer, and ask the user for the time in military, so:
730 = 7:30am
1450 = 2:50pm
etc

6) Data structures. I would say your storage mechanism for classes is perhaps a bit too complicated; I haven't quite read over every piece of code, but what I have read suggests things could be simpler. Have you tried to organize it as:

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
// Where Class represents "Calc 2, id 2014, which meets 3 times a week",
// MeetingTime represents a specific meeting day and range of a class, so 
// "Monday, 2:30-3:30".  A Class will have 1 or more MeetingTime children
struct MeetingTime
{
    // You could store a reference to the 'Class' instance here, but I don't think it's required
    char day;
    int start_time;
    int end_time;
};

struct Class
{
    string name;
    string class_id;
    std::list<MeetingTime> meeting_times;
};

// ...

list<Class> classes;

// Load classes here

// Once loaded, start your processing.  with only 5 potential classes, it won't take too long.  If they entered ~10, you could get a lot of output calculating every permutation!


There are a couple other little things, but this should be enough to get you in the right direction :)
Last edited on
Topic archived. No new replies allowed.