Write a course scheduler using a struct in C++.ask the user for number of classe
ID: 3765913 • Letter: W
Question
Write a course scheduler using a struct in C++.ask the user for number of classes they need to schedule, dynamically create a 1-d array of courses, and then get the information for each course from the user. Your program needs to have the following struct type:
struct course {
string name;
string *days; //dynamic array of days
int start_hr, start_min;
int end_hr, end_min;
};
You will create a 1-d array of structs for classes for someone. For example:
course *courses; //makes pointer to the course type
…
courses=new course[num_courses]; //make array of course type
After you create you array of courses, prompt the user for the information about each course. In order to access each member of the struct, we use the dot operator. In this case, you need to access the array element first, which is the specific course, and then you can access the members of that specific course.
cout << “Enter the name of the course: ”;
cin >> courses[0].name;
Notice that the days member of the struct is a pointer to a string. This is because you do not know how many days each course is offered. It could be 3, 2, or 1 day a week. In addition, you need to make sure there are not any scheduling conflicts. For example, there cannot be an existing Monday class schedule between 11:00 to 11:50 with the example input below:
Enter the name of the course: Cs 193
How many days a week? 3
Enter Day 1: Mon
Enter Day 2: Wed
Enter Day 3: Fri
Enter Start hour and min: 11 0
Enter End hour and min: 11 50
Now, you will offer the user multiple views into this schedule. You can ask how he/she wants to view their schedule. You can list the class information by name, by day, by time, or all, which gives you a M-F schedule. The times for the classes on the same day have to come in the order from earliest to latest in the day. After you print their schedule, then you have to find out if they want a different view, make a new schedule, or exit the program. If they want to make a new schedule, you have to make sure you delete/free the existing schedule before making the new one!!!
Example :
How do you want to view your classes (name, day, time, all)? all
Monday:
CS191: 11:00-11:50
CS190: 2:00-2:50
Tuesday: none
Wednesday:
CS191: 11:00-11:50
CS191: 2:00-2:50
Thursday: none
Friday:
CS191: 11:00-11:50
Do you want to 1-view classes, 2-make new schedule, or 3-exit? 3
Requirements:
functions must not have more than 10 lines of code,not have any global variables, must not have any memory leaks,must detect schedule conflicts, must handle bad input.
Explanation / Answer
Complete Program:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct course
{
string name;
string *days;
int start_hr, start_min;
int end_hr, end_min;
};
string getFullDay(int d)
{
if(d == 0)
return "Monday";
else if(d == 1)
return "Tuesday";
else if(d == 2)
return "Wednesday";
else if(d == 3)
return "Thursday";
else if(d == 4)
return "Friday";
else
return "Week-Off";
}
void printByAll(course *courses, int num_courses, int num_days)
{
for(int i = 0; i < 5; i++)
{
string fullday = getFullDay(i);
cout << fullday << ":";
bool found = false;
for(int j = 0; j < num_courses; j++)
{
for(int k = 0; k < num_days; k++)
{
if(strcmp(fullday.substr(0, 3).c_str(), courses[j].days[k].c_str()) == 0)
{
found = true;
cout.fill('0');
cout << endl << courses[j].name << ": " << courses[j].start_hr << ":" << setw(2) << left << courses[j].start_min << "-" << courses[j].end_hr << ":" << setw(2) << left << courses[j].end_min;
}
}
}
if(found == false)
cout << "none";
cout << endl;
}
}
int main()
{
course *courses;
int num_courses;
string str;
int num_days;
int pos;
int choice;
do
{
cout << "Enter the number of classes: ";
getline(cin, str);
num_courses = stoi(str);
courses = new course[num_courses];
for(int i = 0; i < num_courses; i++)
{
cout << " Enter the name of the course: ";
getline(cin, courses[i].name);
cout << "How many days a week? ";
getline(cin, str);
num_days = stoi(str);
courses[i].days = new string[num_days];
for(int j = 0; j < num_days; j++)
{
cout << "Enter Day " << (j + 1) << ": ";
getline(cin, courses[i].days[j]);
}
cout << "Enter Start hour and min: ";
getline(cin, str);
pos = str.find(" ");
courses[i].start_hr = stoi(str.substr(0, pos + 1));
courses[i].start_min = stoi(str.substr(pos + 1));
cout << "Enter End hour and min: ";
getline(cin, str);
pos = str.find(" ");
courses[i].end_hr = stoi(str.substr(0, pos + 1));
courses[i].end_min = stoi(str.substr(pos + 1));
}
cout << " Do you want to 1-view classes, 2-make new schedule, or 3-exit? ";
getline(cin, str);
choice = stoi(str);
while(choice < 1 || choice > 3)
{
cout << "Do you want to 1-view classes, 2-make new schedule, or 3-exit? ";
getline(cin, str);
choice = stoi(str);
}
if(choice == 1)
{
cout << " How do you want to view your classes (name, day, time, all)? ";
getline(cin, str);
if(strcmp(str.c_str(), "all") == 0)
{
printByAll(courses, num_courses, num_days);
}
else if(strcmp(str.c_str(), "name") == 0)
{
// doing.. printByName(courses, num_courses, num_days);
}
else if(strcmp(str.c_str(), "day") == 0)
{
// doing.. printByDay(courses, num_courses, num_days);
}
else if(strcmp(str.c_str(), "time") == 0)
{
// doing.. printByTime(courses, num_courses, num_days);
}
else
{
cout << "Invalid view option" << endl;
}
}
else if(choice == 2)
{
delete [] courses;
}
else
{
cout << "Thank you" << endl;
}
}while(choice != 3);
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.