#include<iostream> #include<string> using namespace std; int main() { const int
ID: 3603134 • Letter: #
Question
#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");
}
}
This is quest 2 . In this quest, you are going to modify quest 2 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!
My iinstructor wants to add file input and output.
ex. #include<fstream> // you have to include this to the libraray
you have to have fstream fin, fstream > fout, fin.open("filename.txt"), fout.open("filename.txt"), fin.close(), fout.close(), fin >>input variable, fout <<output variable. All this needs to be added to the program I sent you above please. I hope this makes sense.
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
const int MAX_STUDENTS = 30;
const int MAX_ASSIGNMENTS = 10;
cout << "Do you want to load a file(y or n): ";
char choice;
cin >> choice;
int numStudents;
char command = ' ';
int numAssignments = 0;
int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS];
string studentNames[MAX_STUDENTS];
if (choice == 'y') {
ifstream fin("filename.txt");
fin >> numStudents;
fin >> numAssignments;
for (int i = 0; i < numStudents; ++i) {
fin >> studentNames[i];
}
for (int i = 0; i < numStudents; ++i) {
for (int j = 0; j < numAssignments; ++j) {
fin >> assignments[i][j];
}
}
fin.close();
}
else {
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];
}
}
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");
}
ofstream fout("filename.txt");
fout << numStudents << endl;
fout << numAssignments << endl;
for (int i = 0; i < numStudents; ++i) {
fout << studentNames[i] << endl;
}
for (int i = 0; i < numStudents; ++i) {
for (int j = 0; j < numAssignments; ++j) {
fout << assignments[i][j] << " ";
}
fout << endl;
}
fout << endl;
fout.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.