rendered paste body#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct course //struct for course 1 and course 2 sets
{
string name[7];
int ID[7];
int score[7];
};
struct results //struct for results set
{
int ID[7];
int avg[7];
};
// all function prototypes used
int menu();
void initializeCs(course cs[]);
void avg(course cs[], results& rs, int&);
void initializeRes(results& rs);
void passFail(course cs[], results rs, int);
void displayCs(course cs[]);
void displayRes(results rs);
void viewBest(course cs[], results rs, int);
void viewPFile();
void viewFFile();
int main()
{
int choice=0; //declare all variables
course cs[2];
results rs;
int best;
while(choice!=7)
{
choice=menu();
switch(choice) //switch statement for menu items
{
case 1:
initializeCs(cs);
initializeRes(rs);
avg(cs, rs, best);
passFail(cs, rs, best);
break;
case 2:
displayCs(cs);
break;
case 3:
displayRes(rs);
break;
case 4:
viewBest(cs, rs, best);
break;
case 5:
viewPFile();
break;
case 6:
viewFFile();
break;
case 7:
break;
default:
cout << "PLEASE CHOOSE FROM OPTIONS 1-7." << endl << endl;
break;
};
}
return 0;
}
int menu() //Menu for teachers selection
{
int choice;
cout << "1. Create and initialize the arrays COURSE1 and COURSE2." << endl;
cout << "2. Display the current contents of all the arrays COURSE1 and COURSE2." << endl;
cout << "3. Display the Result set." << endl;
cout << "4. View the details of the Best Student." << endl;
cout << "5. View the Pass.dat file." << endl;
cout << "6. View the Fail.dat file." << endl;
cout << "7. Quit." << endl << endl;
cout << "Choose 1-7: ";
cin >> choice;
cout << endl;
return choice;
}
void initializeCs(course cs[]) //array for course 1 and 2 under course cs
{
for (int i=0; i<2; i++)
{
if(i==0)
{
cs[i].name[0]="Alice";
cs[i].name[1]="Bob";
cs[i].name[2]="Chris";
cs[i].name[3]="David";
cs[i].name[4]="Leo";
cs[i].name[5]="Mike";
cs[i].name[6]="Stephen";
// for some reason, i can't store leading zeros without getting errors
/*cs[i].ID[0]=001221;
cs[i].ID[1]=001265;
cs[i].ID[2]=001283;
cs[i].ID[3]=001241;
cs[i].ID[4]=001298;
cs[i].ID[5]=001237;
cs[i].ID[6]=001243;*/
cs[i].ID[0]=1221;
cs[i].ID[1]=1265;
cs[i].ID[2]=1283;
cs[i].ID[3]=1241;
cs[i].ID[4]=1298;
cs[i].ID[5]=1237;
cs[i].ID[6]=1243;
cs[i].score[0]=38;
cs[i].score[1]=47;
cs[i].score[2]=85;
cs[i].score[3]=72;
cs[i].score[4]=40;
cs[i].score[5]=64;
cs[i].score[6]=96;
}
if(i==1)
{
cs[i].name[0]="Alice";
cs[i].name[1]="Mike";
cs[i].name[2]="David";
cs[i].name[3]="Stephen";
cs[i].name[4]="Bob";
cs[i].name[5]="Chris";
cs[i].name[6]="Leo";
// same problem here
/*cs[i].ID[0]=001221;
cs[i].ID[1]=001237;
cs[i].ID[2]=001241;
cs[i].ID[3]=001243;
cs[i].ID[4]=001265;
cs[i].ID[5]=001283;
cs[i].ID[6]=001298;*/
cs[i].ID[0]=1221;
cs[i].ID[1]=1237;
cs[i].ID[2]=1241;
cs[i].ID[3]=1243;
cs[i].ID[4]=1265;
cs[i].ID[5]=1283;
cs[i].ID[6]=1298;
cs[i].score[0]=42;
cs[i].score[1]=55;
cs[i].score[2]=78;
cs[i].score[3]=98;
cs[i].score[4]=45;
cs[i].score[5]=99;
cs[i].score[6]=35;
}
}
cout << "Initialized..." << endl << endl;
return;
}
void initializeRes(results& rs) //array for results
{
// same problem here
/*rs.ID[0]=001221;
rs.ID[1]=001237;
rs.ID[2]=001241;
rs.ID[3]=001243;
rs.ID[4]=001265;
rs.ID[5]=001283;
rs.ID[6]=001298;*/
rs.ID[0]=1221;
rs.ID[1]=1237;
rs.ID[2]=1241;
rs.ID[3]=1243;
rs.ID[4]=1265;
rs.ID[5]=1283;
rs.ID[6]=1298;
return;
}
//function to calculate average score and determine the best student
void avg(course cs[], results& rs, int& best)
{
rs.avg[0] = ( (cs[0].score[0] + cs[1].score[0]) / 2);
best=0;
rs.avg[1] = ( (cs[0].score[5] + cs[1].score[1]) / 2);
if(rs.avg[1]>rs.avg[best])
best=1;
rs.avg[2] = ( (cs[0].score[3] + cs[1].score[2]) / 2);
if(rs.avg[2]>rs.avg[best])
best=2;
rs.avg[3] = ( (cs[0].score[6] + cs[1].score[3]) / 2);
if(rs.avg[3]>rs.avg[best])
best=3;
rs.avg[4] = ( (cs[0].score[1] + cs[1].score[4]) / 2);
if(rs.avg[4]>rs.avg[best])
best=4;
rs.avg[5] = ( (cs[0].score[2] + cs[1].score[5]) / 2);
if(rs.avg[5]>rs.avg[best])
best=5;
rs.avg[6] = ( (cs[0].score[4] + cs[1].score[6]) / 2);
if(rs.avg[6]>rs.avg[best])
best=6;
return;
}
//passes the students who pass or fail to their respective files
void passFail(course cs[], results rs, int best)
{
ofstream fout;
for(int i=0; i<7; i++)
{
if(i==0)
{
if(rs.avg[0]>=45)
fout.open("pass.dat", ios::app);
else
fout.open("fail.dat", ios::app);
fout << cs[0].name[0];
if((rs.avg[0]>=45) && (best==0))
fout << " *** This student received the Best Student award ***" << endl;
else
fout << endl;
fout << rs.ID[0] << endl;
fout << cs[0].score[0] << endl;
fout << cs[1].score[0] << endl;
fout << rs.avg[0];
fout.close();
}
if(i==1)
{
if(rs.avg[1]>=45)
fout.open("pass.dat", ios::app);
else
fout.open("fail.dat", ios::app);
fout << cs[0].name[5];
if((rs.avg[1]>=45) && (best==1))
fout << " *** This student received the Best Student award ***" << endl;
else
fout << endl;
fout << rs.ID[1] << endl;
fout << cs[0].score[5] << endl;
fout << cs[1].score[1] << endl;
fout << rs.avg[1];
fout.close();
}
if(i==2)
{
if(rs.avg[2]>=45)
fout.open("pass.dat", ios::app);
else
fout.open("fail.dat", ios::app);
fout << cs[0].name[3];
if((rs.avg[2]>=45) && (best==2))
fout << " *** This student received the Best Student award ***" << endl;
else
fout << endl;
fout << rs.ID[2] << endl;
fout << cs[0].score[3] << endl;
fout << cs[1].score[2] << endl;
fout << rs.avg[2];
fout.close();
}
if(i==3)
{
if(rs.avg[3]>=45)
fout.open("pass.dat", ios::app);
else
fout.open("fail.dat", ios::app);
fout << cs[0].name[6];
if((rs.avg[3]>=45) && (best==3))
fout << " *** This student received the Best Student award ***" << endl;
else
fout << endl;
fout << rs.ID[3] << endl;
fout << cs[0].score[6] << endl;
fout << cs[1].score[3] << endl;
fout << rs.avg[3];
fout.close();
}
if(i==4)
{
if(rs.avg[4]>=45)
fout.open("pass.dat", ios::app);
else
fout.open("fail.dat", ios::app);
fout << cs[0].name[1];
if((rs.avg[4]>=45) && (best==4))
fout << " *** This student received the Best Student award ***" << endl;
else
fout << endl;
fout << rs.ID[4] << endl;
fout << cs[0].score[1] << endl;
fout << cs[1].score[4] << endl;
fout << rs.avg[4];
fout.close();
}
if(i==5)
{
if(rs.avg[5]>=45)
fout.open("pass.dat", ios::app);
else
fout.open("fail.dat", ios::app);
fout << cs[0].name[2];
if((rs.avg[5]>=45) && (best==5))
fout << " *** This student received the Best Student award ***" << endl;
else
fout << endl;
fout << rs.ID[5] << endl;
fout << cs[0].score[2] << endl;
fout << cs[1].score[5] << endl;
fout << rs.avg[5];
fout.close();
}
if(i==6)
{
if(rs.avg[6]>=45)
fout.open("pass.dat", ios::app);
else
fout.open("fail.dat", ios::app);
fout << cs[0].name[4];
if((rs.avg[6]>=45) && (best==6))
fout << " *** This student received the Best Student award ***" << endl;
else
fout << endl;
fout << rs.ID[6] << endl;
fout << cs[0].score[4] << endl;
fout << cs[1].score[6] << endl;
fout << rs.avg[6];
fout.close();
}
}
return;
}
//function to display the contents of course 1 and course2
void displayCs(course cs[])
{
for(int i=0; i<2; i++)
{
cout << "Course # " << i+1 << ":" << endl << endl;
for(int j=0; j<7; j++)
{
cout << cs[i].name[j] << endl;
cout << "00" << cs[i].ID[j] << endl;
cout << cs[i].score[j] << endl << endl;
}
}
return;
}
//function to display the results
void displayRes(results rs)
{
for(int i=0; i<7; i++)
{
cout << "ID: 00" << rs.ID[i] << endl;
cout << "Average: " << rs.avg[i] << endl << endl;
}
return;
}
//function to view the details of the best student
void viewBest(course cs[], results rs, int best)
{
cout << "The Best Student Award goes to:" << endl << endl;
cout << "Name: " << cs[1].name[best] << endl;
cout << "ID: 00" << rs.ID[best] << endl;
cout << "Course 1 Score: ";
if(best==0)
cout << cs[0].score[0] << endl;
if(best==1)
cout << cs[0].score[5] << endl;
if(best==2)
cout << cs[0].score[3] << endl;
if(best==3)
cout << cs[0].score[6] << endl;
if(best==4)
cout << cs[0].score[1] << endl;
if(best==5)
cout << cs[0].score[2] << endl;
if(best==6)
cout << cs[0].score[4] << endl;
cout << "Course 2 Score: " << cs[1].score[best] << endl;
cout << "Average Score: " << rs.avg[best] << endl << endl;
return;
}
//function to view all the contents of the pass.dat file
void viewPFile()
{
ifstream fin;
string f1;
int f2, f3, f4, f5;
fin.open("pass.dat");
while(!fin.eof())
{
getline(fin, f1);
fin.ignore();
fin >> f2 >> f3 >> f4 >> f5;
cout << "Name: " << f1 << endl;
cout << "ID: 00" << f2 << endl;
cout << "Course 1 Score: " << f3 << endl;
cout << "Course 2 Score: " << f4 << endl;
cout << "Average Score: " << f5 << endl << endl;
};
fin.close();
return;
}
//function to view the contents of the fail.dat file
void viewFFile()
{
ifstream fin;
string f1;
int f2, f3, f4, f5;
fin.open("fail.dat");
while(!fin.eof())
{
getline(fin, f1);
fin.ignore();
fin >> f2 >> f3 >> f4 >> f5;
cout << "Name: " << f1 << endl;
cout << "ID: 00" << f2 << endl;
cout << "Course 1 Score: " << f3 << endl;
cout << "Course 2 Score: " << f4 << endl;
cout << "Average Score: " << f5 << endl << endl;
};
fin.close();
return;
}