QUESTION: In C++, modify the code below so that it saves your class roster to a
ID: 3604494 • Letter: Q
Question
QUESTION: In C++, modify the code below so that it saves your class roster to a file and can load from that file. When you first start the program you should ask if they want to load a file or enter new information! __________________________________________________________________ #include <iostream> #include <string> using namespace std; int main() { const int MAX_STUDENTS = 30; const int MAX_ASSIGNMENTS = 10; cout << "Please enter the number of students currently enrolled (max " << MAX_STUDENTS << "): "; int numStudents; cin >> numStudents; if (numStudents > 30) { numStudents = 30; cout << "The number of students has been reduced to the max " << MAX_STUDENTS << "." << endl; } string studentNames[MAX_STUDENTS]; for (int i = 0; i < numStudents; i++) { cout << "Enter student " << i+1 << "'s name: "; cin >> studentNames[i]; } char command = ' '; int numAssignments = 0; int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS]; while (command != 'q') { system("CLS"); // print the gradebook to screen. for (int i = 0; i < numStudents; i++) { cout << studentNames[i] << " "; if (studentNames[i].size() < 8) { cout << " "; } for (int j = 0; j < numAssignments; j++) { cout << assignments[i][j] << " "; } cout << endl; } cout << "Do you want to add a (s)tudent, add an (a)ssignment, (c)urve a grade (q)uit? "; cin >> command; system("CLS"); switch (command) { case 's': cout << "Please enter the new student's name: "; cin >> studentNames[numStudents]; for (int i = 0; i < numAssignments; i++) { cout << "Please enter " << studentNames[numStudents] << "'s grade for assignment " << i+1 << ": "; cin >> assignments[numStudents][i]; } numStudents++; break; case 'a': for (int i = 0; i < numStudents; i++) { cout << "Please enter " << studentNames[i] << "'s grade for the new assignemnt: "; cin >> assignments[i][numAssignments]; } numAssignments++; break; case 'c': { int assignmentToCurve = -1; cout << "Which Assignment do you wish to curve (1-" << numAssignments << "): "; cin >> assignmentToCurve; //adjust for arrays starting at 0; assignmentToCurve--; int sum = 0; for (int i = 0; i < numStudents; i++) { sum += assignments[i][assignmentToCurve]; } float average = (float)sum / numStudents; if (average < 70) { for (int i = 0; i < numStudents; i++) { assignments[i][assignmentToCurve] += (70 - average); } cout << "curve of " << (int)(70 - average) << " applied!" << endl; } else { cout << "No curve needed!" << endl; } break; } case 'q': break; default: cout << command << " is an invalid command!" << endl; break; } system("PAUSE"); } }
Explanation / Answer
Here is Your Modified program : -------------------->>>>>>>>>>>>>>>>>>>>>>>.
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
int main()
{
ifstream ifile;
ofstream ofile;
int numStudents = 0;
const int MAX_STUDENTS = 30;
const int MAX_ASSIGNMENTS = 10;
string studentNames[MAX_STUDENTS];
char command = ' ';
int numAssignments = 0;
int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS];
cout<<" Do you want to (r)ead From File or (e)nter new information : ";//prompting to ask to read from file
cin>>command;
while(command != 'e')
switch(command){//switching to the read case
case 'r':
{
ifile.open("roast.txt");//openingfile for reading
if(!ifile.is_open()){//reporting error occured in file io
command = 'e';
cout<<" File opening error we are switching you in new information ";
command = 'e';
break;
}
else{
ifile>>numStudents;//getting numStudent value which stored
ifile>>numAssignments;//getting numAssignment value which is stored
for(int i =0;i< numStudents;i++){//storing value to the variable created
ifile>>studentNames[i];
for(int j = 0;j<numAssignments;j++){
ifile>>assignments[i][j];
}
}
ifile.close();//closing file
command = ' ';//assigning default value to goto your main program
goto MainLoop;//return to your main loop
}
}
}
cout<<"";
cout << "Please enter the number of students currently enrolled (max " << MAX_STUDENTS << "): ";
cin >> numStudents;
if (numStudents > 30)
{
numStudents = 30;
cout << "The number of students has been reduced to the max " << MAX_STUDENTS << "." << endl;
}
for (int i = 0; i < numStudents; i++)
{
cout << "Enter student " << i+1 << "'s name: ";
cin >> studentNames[i];
}
MainLoop:
while (command != 'q')
{
system("CLS");
// print the gradebook to screen.
for (int i = 0; i < numStudents; i++)
{
cout << studentNames[i] << " ";
if (studentNames[i].size() < 8)
{
cout << " ";
}
for (int j = 0; j < numAssignments; j++)
{
cout << assignments[i][j] << " ";
}
cout << endl;
}
cout << "Do you want to add a (s)tudent, add an (a)ssignment, (c)urve a grade,(w)rite to file (q)uit? ";
cin >> command;
system("CLS");
switch (command)
{
case 's':
cout << "Please enter the new student's name: ";
cin >> studentNames[numStudents];
for (int i = 0; i < numAssignments; i++)
{
cout << "Please enter " << studentNames[numStudents] << "'s grade for assignment " << i+1 << ": ";
cin >> assignments[numStudents][i];
}
numStudents++;
break;
case 'a':
for (int i = 0; i < numStudents; i++)
{
cout << "Please enter " << studentNames[i] << "'s grade for the new assignemnt: ";
cin >> assignments[i][numAssignments];
}
numAssignments++;
break;
case 'c':
{
int assignmentToCurve = -1;
cout << "Which Assignment do you wish to curve (1-" << numAssignments << "): ";
cin >> assignmentToCurve;
//adjust for arrays starting at 0;
assignmentToCurve--;
int sum = 0;
for (int i = 0; i < numStudents; i++)
{
sum += assignments[i][assignmentToCurve];
}
float average = (float)sum / numStudents;
if (average < 70)
{
for (int i = 0; i < numStudents; i++)
{
assignments[i][assignmentToCurve] += (70 - average);
}
cout << "curve of " << (int)(70 - average) << " applied!" << endl;
}
else
{
cout << "No curve needed!" << endl;
}
break;
}
case 'q':
break;
case 'w': //saving the data to the roast.txt file
ofile.open("roast.txt");
if(!ofile.is_open()){
cout<<" File Opening Error ";
command = 'q';
ofile.close();
break;
}
else{
ofile<<numStudents<<" "<<numAssignments<<" ";
for(int i = 0; i<numStudents;i++){
ofile<<studentNames[i]<<" ";
for(int j = 0;j< numAssignments;j++){
ofile<<assignments[i][j]<<" ";
}
ofile<<" ";
}
ofile.close();
break;
}
default:
cout << command << " is an invalid command!" << endl;
break;
}
system("PAUSE");
}
}