Question: Help with a little fix for my program. in this function, calculate the expected size of the file it's writing, and output that like this,

Help with a little fix for my program.

in this function, calculate the expected size of the file it's writing, and output that like this, substituting the computed size where xxx appears:

 Writing courses.dat, xxx bytes... 

I think that it needs to output "writing the name of the file and the bytes" but how? In main or in the function. Thanks

#include

using std::cout;

using std::cin;

using std::endl;

using std::ios;

//header necessary to provide parametric manipulators to set the field

//in order to display the courses as a list

#include

using std::setw;

using std::left;

#include

using std::fstream;

//header necessary to use 'atoi' and convert the elements in 'buf' to integers

#include

//header necessary to manipulate C strings and arrays such as 'toupper' used in the program

#include

//'struct' definition created called 'course' to have the name, term, units, grade elements and the pointer to

//use it as a linked list

struct course

{

char name[11]; //array character 'name' to display the course as subject code, a dash, and a sequence number

char term[7]; //array character 'term' to display the 6-character code of the course

int units; //array integer 'units' to display the number of course units

char grade; //array character 'grade' to display the letter grade

course* next; //necessary so that the struct can be used in a linked list, by adding next to the pointer

};

//function prototype(s)

void displayCourses(course*); //function necessary to point to the struct course and display the course list

void restoreList(course*&);

void saveList(course*&);

int getNodeCount(course*&);

void sortByUnit(course*&);

void sortByGrade(course*&);

void sortByDescription(course*&);

void sortByTerm(course*&);

int main()

{

course* start = 0;

restoreList(start);

while (true)

{

char buf[100]; //character array 'buf' to use as buffer with a limit of 100 characters to store the user input

displayCourses(start); //declare an empty simple linked list

cout << endl << "Would you like to add another course? (Y/N) ";

cin >> buf; //gets the user input and store it in 'buf'

cin.ignore(1000, 10); //necessary to process the ENTER key symbol in the input buffer

if (toupper(buf[0]) == 'N') break; //if the user chooses 'N' or 'n' for no, break the loop

//if the user chooses yes, the program will point to the struct course and it will add a new course

course* t = new course;

//the program displays to the user to enter the course name,

//gets the user input for course name and stores it in 'name', in the struct that is pointed by s

cout << "Enter the course name ex: comsc-165 (MAX 10 chars): ";

cin.getline(t->name, 100);

//the program displays to the user to enter the term and

//gets the user input for term and stores it in 'term', in the struct that is pointed by s

cout << "Enter the term ex: FA2017 (MAX 6 chars): ";

cin.getline(t->term, 100);

//the program displays to enter the number of units and

//gets the user input for 'term' and stores it in the buffer. Later, is converted as integer with 'atoi'

//gathered from the buffer and stored the number of units in the units that is in the struct object

//that is pointed by s

cout << "Enter the number of units (ex: 3): ";

cin >> buf; t->units = atoi(buf);

//the program displays to enter the grade from the course and

//gets the user input for grades and stores it in buffer. Later, is converted to upper case letter

//by the cstring 'toupper' after it is gathered from the buffer and stored it to 'grade' in the struct object

// that it is pointed by s

cout << "Enter the grade (ex: A or b): ";

cin >> buf; t->grade = toupper(buf[0]);

course* p, *prev;

for (p = start, prev = 0; p; prev = p, p = p->next);

t->next = p;

if (prev)

prev->next = t;

else

start = t;

}

saveList(start);

cout << endl << "By Unit..." << endl;

sortByUnit(start);

displayCourses(start);

cout << "Press Enter to Continue";

cin.ignore();

cout << endl << "By Grade..." << endl;

sortByGrade(start);

displayCourses(start);

cout << "Press Enter to Continue";

cin.ignore();

cout << endl << "By Description..." << endl;

sortByDescription(start);

displayCourses(start);

cout << "Press Enter to Continue";

cin.ignore();

cout << endl << "By Term..." << endl;

sortByTerm(start);

displayCourses(start);

cout << "Press Enter to Continue";

cin.ignore();

//before the program ends, the while loop traverses the linked list and allocate the memory

//for the 'tod' node. Later it deallocates the memory and return the memory so that it does not

//hold anything. Then, it is used for each of the objects (because they were instantiated with the new keyword).

while (start)

{

course* p = start;

start = start->next;

delete p;

}

}

//used setw() so that the output looks well-presented, organized, and easy-to-read.

//The void function takes the start pointer as the only parameter in the parameter list.

void displayCourses(course* t)

{

cout << left << setw(12) << "COURSE"

<< setw(8) << "TERM"

<< setw(7) << "UNITS"

<< setw(7) << "GRADE" << endl;

cout << left << setw(12) << "----------"

<< setw(8) << "------"

<< setw(7) << "-----"

<< setw(7) << "-----" << endl;

//'loop for' to display the term, units and grade in a well-organized traversal list.

//It goes through each linked list node

for (const course* p = t; p; p = p->next)

{

cout << left << setw(12) << p->name //prints the name of the courses that are pointed by p, gathered from the user

<< setw(8) << p->term //prints the term from the courses that are pointed by p, gathered from the user

<< setw(7) << p->units //prints the units from the courses that are pointed by p, gathered from the user

<< setw(7) << p->grade << endl; //prints the grade from the courses that are pointed by p, gathered from the user

}

}

void restoreList(course*& start) {

char buf[100];

fstream fin;

fin.open("courses.dat", ios::binary | ios::in);

if (!fin) return;

else {

cout << "Would you like to load course data from the previous session? (Y/N) ";

cin >> buf;

cin.ignore(1000, 10);

if (toupper(buf[0]) == 'N') return;

}

// read the number of objects from the disk file

int nRecs;

fin.read((char*)&nRecs, sizeof(int));

// read the objects from the disk file

for (int i = 0; i < nRecs; i++) {

course* t = new course;

fin.read((char*)t, sizeof(course));

course* p, *prev;

for (p = start, prev = 0; p; prev = p, p = p->next);

t->next = p;

if (prev)

prev->next = t;

else

start = t;

}

fin.close();

}

void saveList(course*& start) {

fstream fout;

fout.open("courses.dat", ios::binary | ios::out);

int nRecs = getNodeCount(start);

fout.write((char*)&nRecs, sizeof(int));

for (const course* p = start; p; p = p->next) {

fout.write((char*)p, sizeof(course));

}

fout.close();

}

int getNodeCount(course*& start) {

int count = 0;

for (const course* p = start; p; p = p->next) {

count++;

}

return count;

}

void sortByUnit(course*& start)

{

course* newStart = 0;

while (start)

{

course* t = start;

start = start->next;

course* p;

course* prev;

for (p = newStart, prev = 0; p; prev = p, p = p->next)

if (t->units < p->units)

break;

t->next = p;

if (prev)

prev->next = t;

else

newStart = t;

}

start = newStart;

}

void sortByGrade(course*& start)

{

course* newStart = 0;

while (start)

{

course* t = start;

start = start->next;

course* p;

course* prev;

for (p = newStart, prev = 0; p; prev = p, p = p->next)

if (t->grade < p->grade)

break;

t->next = p;

if (prev)

prev->next = t;

else

newStart = t;

}

start = newStart;

}

void sortByDescription(course*& start)

{

course* newStart = 0;

while (start)

{

course* t = start;

start = start->next;

course* p;

course* prev;

for (p = newStart, prev = 0; p; prev = p, p = p->next)

if (t->name < p->name)

break;

t->next = p;

if (prev)

prev->next = t;

else

newStart = t;

}

start = newStart;

}

void sortByTerm(course*& start)

{

course* newStart = 0;

while (start)

{

course* t = start;

start = start->next;

course* p;

course *prev;

for (p = newStart, prev = 0; p; prev = p, p = p->next)

if (t->term < p->term)

break;

t->next = p;

if (prev)

prev->next = t;

else

newStart = t;

}

start = newStart;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!