Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
class Student //abstract data type implemented as a class
{
public: //prototypes for public member functions
Student(); //constructor function
void setID(int);
void setFirstName(string);
void setLastName(string);
void setQuiz1(int);
void setQuiz2(int);
void setMidterm(int);
void setFinal(int);
int getTotalpts();
double getpercent();
char getgrade();
private: //private data members accessible to member functions
int studentID;
string first_name;
string last_name;
int quiz_1;
int quiz_2;
int midterm_score;
int final_score;
int totalpts;
double percent;
char grade;
};
Student::Student() //default constructor
{
//do nothing
}
//member functions
void Student::setID(int id)
{
studentID = id;
} //end setID function
void Student::setFirstName(string first)
{
first_name = first;
} //end setFirstName function
void Student::setLastName(string last)
{
last_name = last;
} //end setLastName function
void Student::setQuiz1(int quiz1)
{
quiz_1 = quiz1;
} //end setQuiz1 function
void Student::setQuiz2(int quiz2)
{
quiz_2 = quiz2;
} //end setQuiz2 function
void Student::setMidterm(int midterm)
{
midterm_score = midterm;
} //end setMidterm function
void Student::setFinal(int final)
{
final_score = final;
} //end setFinal function
int Student::getTotalpts()
{
//calculate totalpts
totalpts = quiz_1 + quiz_2 + midterm_score + final_score;
return totalpts;
} //end gettotalpts()
double Student::getpercent()
{
//calculate percent
percent = (totalpts)*100/200;
return percent;
} //end getpercent()
char Student::getgrade()
{
//calculate grade
if (percent >= 90)
return 'A';
else if (percent >= 80)
return 'B';
else if (percent >= 70)
return 'C';
else if (percent >= 60)
return 'D';
else return 'F';
} //end getgrade()
int main()
{
void getInput(int &ID, string &firstName, string &lastName, int &firstQuiz,
int &secondQuiz, int &midterm, int &final);
bool moreStudents();
void displayRecord(int ID, string firstName, string lastName, int firstQuiz,
int secondQuiz, int midterm, int final, int total,
double percentage, char finalgrade);
int computeHighestPoints(int numbers1, int numbers2);
int computeLowestPoints(int numbers1, int numbers2);
double computeAveragePoints(int allPoints, int totalStudents);
double computeAveragePercent (double avgPoints, int);
char computeGrade(double input);
void displayTotals(int totalStudents, double highest, double lowest,
double avgPoints, char avgGrade);
//declare variables
//input
int studentID;
string first_name;
string last_name;
int quiz_1;
int quiz_2;
int midterm_score;
int final_score;
//calculation results
int total_pts;
double percent;
char grade;
//totals
int totalStudents = 0;
int highest = 0;
int lowest = 0;
int allPoints = 0;
double avgPoints;
int maxpts = 200;
double avgPercent;
char avgGrade;
Student person;
do
{
getInput(studentID, first_name, last_name, quiz_1, quiz_2, midterm_score, final_score);
//set input values
person.setID(studentID);
person.setFirstName(first_name);
person.setLastName(last_name);
person.setQuiz1(quiz_1);
person.setQuiz2(quiz_2);
person.setMidterm(midterm_score);
person.setFinal(final_score);
//perform calculations
total_pts = person.getTotalpts();
percent = person.getpercent();
grade = person.getgrade();
lowest = computeLowestPoints(person.getTotalpts(), highest);
highest = computeHighestPoints(highest, person.getTotalpts());
totalStudents++; //adds 1 to totalStudents counter
allPoints = allPoints + person.getTotalpts(); //accumulates total class
avgPoints = computeAveragePoints(allPoints, totalStudents);
avgPercent = computeAveragePercent(avgPoints, maxpts);
avgGrade = computeGrade(avgPercent);
ofstream iFile("students.txt", ios::app);
if (!iFile)
{
cout << "Error: Couldn't Append To End Of File" << endl;
}else{
iFile << "\n\nStudent ID: " << studentID
<< "\nFirst Name: " << first_name
<< "\nLast Name: " << last_name
<< "\nQuiz #1 Score: " << quiz_1
<< "\nQuiz#2 Score: " << quiz_2
<< "\nMidterm Score: " << midterm_score
<< "\nFinal Score: " << final_score
<< "\nTotal Points Earned: " << total_pts
<< "\nOverall Percentage: " << percent << "%"
<< "\nLetter Grade: " << grade;
}
cin.get();
iFile.close();
}while(moreStudents()); //calls the moreStudents() function
ifstream iFile("students.txt");
char ch;
while(!iFile.eof())
{
iFile.get(ch);
cout << ch;
}
iFile.close();
displayTotals(totalStudents, highest, lowest, avgPoints, avgGrade);
system("PAUSE");
return 0;
} //end main()
bool moreStudents()
{
bool more = false;
char anotherStudent = 'n';
cout << "\n\nWould You like to enter another student? enter 'y' or 'Y' and press ENTER; \n";
cout << "if not, enter any other character and press ENTER ";
cin >> anotherStudent;
if(anotherStudent == 'y' || anotherStudent == 'Y')
more = true;
else
more = false;
return more;
} //end anotherStudent
void getInput(int &ID, string &firstName, string &lastName, int &firstQuiz,
int &secondQuiz, int &midterm, int &final)
{
string message = "\n\nEnter Student ID: ";
do
{
cout << message;
cin >> ID;
message = "\n\nError! Please Re-Enter Student ID (1-99999): ";
} while (ID < 1 || ID > 99999);
//asks user to input first and last name
cout << "Enter First Name: ";
cin >> firstName;
cout << "Enter Last Name: ";
cin >> lastName;
//determines quiz #1 score
string message1 = "Please enter Quiz #1 score: ";
do
{
cout << message1;
cin >> firstQuiz;
message1 = "\n\nError! Please Re-Enter Quiz #1 score: ";
} while (firstQuiz < 0 || firstQuiz > 25);
string message2 = "Please enter Quiz #2 score: ";
do
{
cout << message2;
cin >> secondQuiz;
message2 = "\n\nError! Please Re-Enter Quiz #2 2core: ";
} while (secondQuiz < 0 || secondQuiz > 25);
string message3 = "Please enter Midterm score: ";
do
{
cout << message3;
cin >> midterm;
message3 = "\n\nError! Please Re-Enter Midterm 2core: ";
} while (midterm < 0 || midterm > 50);
string message4 = "Please enter Final score: ";
do
{
cout << message4;
cin >> final;
message4 = "\n\nError! Please Re-Enter Final score: ";
} while (final < 0 || final > 100);
return;
} //getInput()
int computeLowestPoints(int numbers1, int numbers2)
{
if (numbers1 <=numbers2)
return numbers1;
else
return numbers2;
}//end computeLowestPoints
int computeHighestPoints(int numbers1, int numbers2)
{
if (numbers1 >= numbers2)
return numbers1;
else
return numbers2;
}//end computeHighestPoints
double computeAveragePoints(int all, int total)
{
int output;
output = all/total;
return output;
}//end computeAveragePoints
double computeAveragePercent(double avg, int max)
{
//calculates percentage
double output;
output = (avg)*100/max;
return output;
} //end computePercent
char computeGrade(double input)
{
if (input >= 90)
return 'A';
else if (input >= 80)
return 'B';
else if (input >= 70)
return 'C';
else if (input >= 60)
return 'D';
else return 'F';
}//end computeGrade
void displayTotals(int totalStudents, double highest, double lowest,
double avgPoints, char avgGrade)
{
cout << "\n-------------------------------------------------\n\n";
cout << "Total Students = " << totalStudents;
cout << "\nHighest Total Points Earned = " << highest;
cout << "\nLowest Total Points Earned = " << lowest;
cout << "\nAverage Total Points Earned = " << avgPoints;
cout << "\nAverage Letter Grade Earned = " << avgGrade;
cout << "\n-------------------------------------------------\n\n";
} //end displayTotals