Write a menu-driven C++ program to manage your college course history and plans,
ID: 3732396 • Letter: W
Question
Write a menu-driven C++ program to manage your college course history and plans, named as you wish. Go back to the module on C/C++ strings, because you're going to need C strings in this one.
It should work something like this:
Requirements
Use a dynamic array of struct-based objects.
Allow for course names up to 12 characters in length, like Comsc-150sql. Do not use C++ strings -- they will not serialize to a binary file. Use only C strings in the struct.
Convert course names and grades entries to uppercase as they are inputted.
Allow units to be whole numbers or floating point, per your choice.
Allow grade to be a single char (like A, B, etc) or a C string (like A+, B-, etc), per your choice.
Use a dynamic array of objects to store the course information, with an initial capacity of 2.
Double the array capacity when (a) you have a new object to add and (b) size equals capacity.
The output table should have nicely-spaced column headings -- refer to what you learned about cout.width.
Output the array size and capacity along with the output table.
Output blank lines to separate blocks of text as modeled in the sample output above.
Serialize using a binary file named courses.dat.
How To Arrange An Array Of Objects
Remember how to sort the integer arrays in previous lectures:
For high-to-low, simply exchange the i and j in the if-statement. swap is in the C++ algorithm library.
Functions
You don't have to write functions for everything. You may just write code blocks in main, and if it makes sense for you to move any of them out of main and into functions (like a void function to cout a table of courses), do so. Make use of the functions provided for you to copy, paste, and adapt, found in this module and the module on strings.
// sort the values lo-to-hi for (int i = 0; i < CAPACITY; i++) for (int j = i + 1; j < CAPACITY; j++) if (scores[j] < scores[i]) swap(scores[i], scores[j]);
Explanation / Answer
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <string.h>
using namespace std;
class Course
{
private:
char courseName[12];
int courseYear;
int courseUnit;
char courseGrade;
public:
void SetCourseName(char courseName[12])
{
strcpy(this->courseName, courseName);
}
void SetCourseYear(int courseYear)
{
this->courseYear = courseYear;
}
void SetCourseUnit(int courseUnit)
{
this->courseUnit = courseUnit;
}
void SetCourseGrade(int courseGrade)
{
this->courseGrade = courseGrade;
}
char* GetCourseName()
{
return this->courseName;
}
int GetCourseYear()
{
return this->courseYear;
}
int GetCourseUnit()
{
return this->courseUnit;
}
char GetCourseGrade()
{
return this->courseGrade;
}
};
int main()
{
int size = 2, courseObjectCount = 0;
Course* couseArray = new Course[size];
while(true)
{
cout<<"Array size: "<<courseObjectCount<< " capacity: "<<size<<endl;
char enteredWord;
cout<<"MENU"<< endl;
cout<<"Press A ..Add a course"<< endl;
cout<<"Press L ..List all courses"<< endl;
cout<<"Press C ..Arrange by course"<< endl;
cout<<"Press Y ..arrange by Year"<< endl;
cout<<"Press U ..arrange by Units"<< endl;
cout<<"Press G ..arrange by Grade"<< endl;
cout<<"Press Q ..Quit"<< endl;
cin>>enteredWord;
if(enteredWord == 'A')
{
//Double the Size
if(size == courseObjectCount)
{
size = size*2;
Course* newCouseArray = new Course[size];
for(int t = 0 ; t < courseObjectCount; t++)
{
newCouseArray[t] = couseArray[t];
}
delete[] couseArray;
couseArray = newCouseArray;
}
// Enter the details from User
char courseName[12];
cout<<"Enter a courses' name:"<< endl;
cin>>courseName;
int upparChar =0;
while(courseName[upparChar]) {
courseName[upparChar] = toupper(courseName[upparChar]);
upparChar++;
}
int courseYear;
cout<<"Enter the year for "<<courseName<<" [like 2016]:"<<endl;
cin>>courseYear;
int courseUnit;
cout<<"Enter the units for "<<courseName<<" [0, 1, 2,...]:"<<endl;
cin>>courseUnit;
char courseGrade;
cout<<"Enter the grade for "<<courseName<<" [A, B,..., X if still in progress or planned]:"<<endl;
cin>>courseGrade;
courseGrade = toupper(courseGrade);
Course newAddedCourse;
newAddedCourse.SetCourseName(courseName);
newAddedCourse.SetCourseYear(courseYear);
newAddedCourse.SetCourseUnit(courseUnit);
newAddedCourse.SetCourseGrade(courseGrade);
couseArray[courseObjectCount] = newAddedCourse;
courseObjectCount++;
}
else if(enteredWord == 'L')
{
cout<<("Course Year Units Grade")<<endl;
for(int t = 0 ; t < courseObjectCount; t++)
{
printf("%-12s%-5d%-6d%c",couseArray[t].GetCourseName(),couseArray[t].GetCourseYear(),
couseArray[t].GetCourseUnit(),couseArray[t].GetCourseGrade());
printf(" ");
}
}
else if(enteredWord == 'C')
{
// sort the values hi-to-lo
for (int i = 0; i < courseObjectCount; i++)
{
for (int j = i + 1; j < courseObjectCount; j++)
{
if (couseArray[i].GetCourseName() < couseArray[j].GetCourseName())
swap(couseArray[i], couseArray[j]);
}
}
}
else if(enteredWord == 'Y')
{
// sort the values hi-to-lo
for (int i = 0; i < courseObjectCount; i++)
{
for (int j = i + 1; j < courseObjectCount; j++)
{
if (couseArray[i].GetCourseYear() < couseArray[j].GetCourseYear())
swap(couseArray[i], couseArray[j]);
}
}
}
else if(enteredWord == 'U')
{
// sort the values hi-to-lo
for (int i = 0; i < courseObjectCount; i++)
{
for (int j = i + 1; j < courseObjectCount; j++)
{
if (couseArray[i].GetCourseUnit() < couseArray[j].GetCourseUnit())
swap(couseArray[i], couseArray[j]);
}
}
}
else if(enteredWord == 'G')
{
// sort the values hi-to-lo
for (int i = 0; i < courseObjectCount; i++)
{
for (int j = i + 1; j < courseObjectCount; j++)
{
if (couseArray[i].GetCourseGrade() < couseArray[j].GetCourseGrade())
swap(couseArray[i], couseArray[j]);
}
}
}
else if(enteredWord == 'Q')
{
break;
}
}
return 0;
}
//PLEASE PROVIDE FEEDBACK
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.