In: Computer Science
Write a C++ console program that prompts a user to enter information for the college courses you have completed, planned, or are in progress, and outputs it to a nicely-formatted table. Name the CPP as you wish (only use characters, underscores and number in your file name. DO NOT use blank).
Its output should look something like this:
Course Year Units Grade ---------- ---- ----- ----- comsc-110 2015 4 A comsc-165 2016 4 ? comsc-200 2016 4 ? comsc-155h 2014 4 A comsc-260 2017 4 ? bus-90 2015 4 A
Here are the requirements:
And for structural requirements:
Input/Output Sample:
A session might look like this, with user input in bold blue with [ENTER] to signify the Enter or Return key pressed, so that it's easier to see in the sample below. Note that it starts by outputting an empty table:
Course Year Units Grade ----------- ---- ------ ----- Enter course #1 [Q to exit]: Comsc-165[ENTER] What year for Comsc-165? [e.g., 2016]: 2016[ENTER] How many units is Comsc-165? 4[ENTER] And what was your grade [? for in-progress or planned]: ?[ENTER] Course Year Units Grade ----------- ---- ------ ----- comsc-165 2016 4 ? Enter course #2 [Q to exit]: Comsc-110[ENTER] What year for Comsc-110? [e.g., 2016]: 2015[ENTER] How many units is Comsc-110? 4[ENTER] And what was your grade [? for in-progress or planned]: A[ENTER] Course Year Units Grade ----------- ---- ------ ----- comsc-165 2016 4 ? comsc-110 2015 4 A Enter course #3 [Q to exit]: q[ENTER]
Because this uses serialization, the above 2 courses should be serialized down (that is, saved) to a TXT file, and serialized up (that is, restored) the next time the program is executed. In that case, the program would start like this:
Course Year Units Grade ----------- ---- ------ ----- comsc-165 2016 4 ? comsc-110 2015 4 A Enter course #3 [Q to exit]:
The user could then quit right away, using the program only to view the already-entered courses, or add to them.
This is just a sample. Yours should work the same way, with the same sequence of inputs (course, year, units, grade), and be nicely formatted and spaced. It should be something you'd be proud to show in a job interview.
Program:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//structure of Course
struct Course
{
string course;
int year;
double units;
string grade;
};
//function to read the data for a single course, and return a
filled-in Course object
Course cinOneCourse(int i)
{
Course c;
cout<<"Enter course#"<<i<<"[Q to exit]: ";
cin >>c.course;
if(c.course!="Q" && c.course!="q")
{
cout<<"What year for "<<c.course<<"?[e.g., 2016]:
";
cin >>c.year;
cout<<"How many units in "<<c.course<<"? ";
cin >>c.units;
cout<<"And what was your grade [? for in-progress or
planned]: ";
cin >>c.grade;
}
return c;
}
//function to print the table header and all the in-use elements
of the Course object array
void coutAllCourses(Course c[], int n)
{
cout<<endl;
cout<<left<<setw(20)<<"Course"<<left<<setw(10)<<"Year"<<left<<setw(10)<<"Units"<<"Grade"<<endl;
cout<<left<<setw(20)<<"------"<<left<<setw(10)<<"----"<<left<<setw(10)<<"-----"<<"-----"<<endl;
for(int i=0; i<n; i++)
{
cout<<left<<setw(20)<<c[i].course<<left<<setw(10)<<c[i].year
<<left<<setw(10)<<c[i].units<<c[i].grade<<endl;
}
cout<<endl;
}
//main function
int main()
{
//an object array of capacity 100
Course course[100];
int n = 0;
ifstream fin;
//open the file
fin.open("courses.txt");
//if file exist then read the data from this file and copy the data
into the array
if(fin.is_open())
{
for(n=0; ; n++)
{
fin>>course[n].course;
//check for end-of-file
if(fin.eof())
break;
fin>>course[n].year>>course[n].units>>course[n].grade;
}
}
//read the data for courses
for(int i=n; i<100; i++)
{
course[i] = cinOneCourse(i+1);
//check if user pressed Q
if(course[i].course=="Q" || course[i].course=="q")
break;
//print the data
coutAllCourses(course, i+1);
n++;
}
//close the file
fin.close();
ofstream fout;
//open the file to write
fout.open("courses.txt");
//write the data to the file
for(int i=0; i<n; i++)
{
fout<<left<<setw(20)<<course[i].course<<left<<setw(10)<<course[i].year
<<left<<setw(10)<<course[i].units<<course[i].grade<<endl;
}
//close the file
fout.close();
return 0;
}
Output:
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid
plagiarism.
Thank you.