Question

In: Computer Science

Write a C++ console program that prompts a user to enter information for the college courses...

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:

  1. Use a struct named Course with the 4 attributes needed for the table, named as you wish.
  2. Use int or double for Units (your choice) and char or string for Grade (your choice).
  3. Use ? or X or some other designation for Grades for courses in progress or planned (your choice).
  4. Track the number of objects in use in the partially filled array -- that is, the size.
  5. Use an object array of capacity 100 in the main program to track up to 100 course objects.
  6. Include table column headings, correctly-spaced.
  7. The total of all column widths should not exceed 80 spaces.
  8. You choose which attributes to left-justify, right-justify, or center.
  9. Use the string buffer method to cin numbers.
  10. Serialize using a text file named "courses.txt". At the beginning of your program, you need to check if this file "courses.txt" exists. If it does, you read the data from this file and copy the data into your array. When your program is run for the first time, this file does not exists. But, it should exist on subsequent runs. Before your program exits, you need to save your course array to "courses.txt" file.
  11. Utilize at least 2 functions.

And for structural requirements:

  1. Never use global variables. Do not use global constants in this one, because they are not needed.
  2. At your option, write a value-returning function of the prototype: Course cinOneCourse(int); to prompt the user for a single course, and return a filled-in Course object. The parameter's value is a sequence number: 0 or 1 for the first student, 1 or 2 for the next -- you decide whether to start at 0 or 1 in the input prompts. In main, copy the returned object to the next unused element of the array. You may use a for-loop in main to manage this.
  3. At your option, write a void function with this prototype: void coutAllCourses(Course[ ], int); to cout the table header and all the in-use elements of the Course object array. Call the function from main, after each time a new course is added to the array. Or write it all in code blocks in main.
  4. To end the for-loop before 100 courses get entered, allow the user to q or Q for the course. In the cinOneCourse function (or code block), skip input of the remaining attributes if the course is lowercase q or uppercase Q. In the for-loop in main that controls all this, break from the loop before calling the coutAllCourses (or code block) again if a q or Q course name is detected, and that ends the program.

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.

Solutions

Expert Solution

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.


Related Solutions

C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a C program that prompts the user to enter some information about up to 20...
Write a C program that prompts the user to enter some information about up to 20 individuals (think of a way to welcome and prompt the user). It must be stored in a structure. Once data is entered, the program output it as shown in sample run below. Your program should include a structure with a tag name of: “information”. It should contain the following data as members: a struct to store employee's name, defined as: struct name fullname e.g....
Write a program in c++ using only if statements that prompts the user to enter an...
Write a program in c++ using only if statements that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1 …., and Saturday is 6) then displays today. Also, prompt the user to enter the number of days after today for a future day and display the future day of the week. The future day can be computed as follows: (today + number of days after today) % 7 Sample run...
C# Create a console application that prompts the user to enter a regular expression, and then...
C# Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a regular expression...
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
C programming. Write a program that prompts the user to enter a 6x6 array with 0...
C programming. Write a program that prompts the user to enter a 6x6 array with 0 and 1, displays the matrix, and checks if every row and every column have the even number of 1’s.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT