Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iomanip>
- using namespace std;
- class Student{
- private:
- string name, ssn, sex, fileWithPath, filePath, defaultFile;
- unsigned short int age, transferHours, transferQualityPoints, msuHours, msuQualityPoints;
- float tGPA, msuGPA, combinedGPA;
- public:
- Student(string name, unsigned short int age, string ssn, string sex,
- unsigned short int transferHours, unsigned short int transferQualityPoints,
- unsigned short int msuHours, unsigned short int msuQualityPoints){
- Student::name = name;
- Student::age = age;
- Student::ssn = ssn;
- Student::sex = sex;
- Student::transferHours = transferHours;
- Student::transferQualityPoints = transferQualityPoints;
- Student::msuHours = msuHours;
- Student::msuQualityPoints = msuQualityPoints;
- Student::fileWithPath = fileWithPath;
- Student::filePath = filePath;
- Student::defaultFile = defaultFile;
- calcGPA();
- };
- Student(){
- string studentName, studentSSN, studentSex, fileWithPathString, filePathString;
- name = "Leeroy Jenkins";
- ssn= "XXX-xx-XXXX";
- sex= "Sex";
- defaultFile= "info.txt";
- filePath = "/Users/leeroy/Desktop/eerk/";
- // filePath = "C:\\Documents and Settings\\Leeroy\\Desktop\\";
- fileWithPath = defaultFile + filePath;
- age = 24;
- transferHours=12;
- transferQualityPoints=47;
- msuHours=48;
- msuQualityPoints=120;
- calcGPA();
- }
- ~Student(){};
- void getStudent(){
- printf("Please Enter First and Last Name: ");
- getline(cin, name);
- printf("Please Enter SSN (123-45-6789): ");
- getline(cin, ssn);
- printf("Please Enter Sex: ");
- getline(cin, sex);
- printf("Please Enter Age: ");
- cin >> age;
- printf("Please Enter Transfer Credit Hours: ");
- cin >> transferHours;
- printf("Please Enter Transfer Quality Points: ");
- cin >> transferQualityPoints;
- printf("Please Enter MSU Credit Hours: ");
- cin >> msuHours;
- printf("Please Enter MSU Quality Points: ");
- cin >> msuQualityPoints;
- cin.ignore();
- calcGPA();
- };
- bool getStudentFromFile(string fileName){
- string firstName,lastName,aLine, aWord;
- ifstream studentInfo(fileName.c_str());
- if (!studentInfo){
- printf("\n\n\t----Failed to intilize file----\n\t\t Aborting\n");
- return 0;
- }
- else {
- studentInfo>>aWord;
- studentInfo>> firstName>> lastName;
- name = firstName + " " + lastName;
- studentInfo>>aWord;
- studentInfo>> age;
- studentInfo>>aWord;
- studentInfo>>ssn;
- studentInfo>>aWord;
- studentInfo>>sex;
- studentInfo>>aWord; studentInfo>>aWord;
- studentInfo>>transferHours;
- studentInfo>>aWord; studentInfo>>aWord; studentInfo>>aWord;
- studentInfo>>transferQualityPoints;
- studentInfo>>aWord; studentInfo>>aWord;
- studentInfo>>msuHours;
- studentInfo>>aWord; studentInfo>>aWord; studentInfo>>aWord;
- studentInfo>>msuQualityPoints;
- calcGPA();
- return 1;
- };
- };
- float calcGPA(){
- int combinedCreditHours, combinedQualityPoints;
- tGPA = static_cast<float>(transferQualityPoints)/transferHours;
- msuGPA = static_cast<float>(msuQualityPoints)/msuHours;
- combinedCreditHours= transferHours + msuHours;
- combinedQualityPoints= transferQualityPoints + msuQualityPoints;
- combinedGPA= static_cast<float>(combinedQualityPoints)/combinedCreditHours;
- return 1;
- };
- void printStudent(){
- cout << "\n\n *********************************************\n\n"
- << "Name: " << name << endl
- << "Age: " << age << endl
- << "SSN: " << ssn << endl
- << "Sex: " << sex << endl << endl
- << "Transfer Hours: " << setw(3) << transferHours
- << "\t Transfer Quality Points: " << setw(3) << right <<transferQualityPoints << endl
- << "MSU Hours: " << right <<setw(3) << msuHours
- << "\t MSU Quality Points: " << right << setw(3) <<msuQualityPoints << endl<< endl
- << "Transfer GPA: "<<fixed<<setw(5)<<setprecision(2)<< right << tGPA << endl
- << "MSU GPA: "<< fixed<<setw(5)<<setprecision(2)<< msuGPA << endl
- << "Total GPA: "<< fixed<<setw(5)<<setprecision(2)<< combinedGPA << endl
- << "\n *********************************************\n\n";
- cin.ignore();
- };
- void printStudentToFile(bool defaultSSN){
- if (defaultSSN) fileWithPath = filePath + ssn + ".txt";
- else fileWithPath = filePath + "record.txt";
- ofstream studentInfoFile(fileWithPath.c_str());
- if (!studentInfoFile) printf("\n\n\t----Failed to create file----\n\t \t Aborting\n");
- else cout << "Saving to " << fileWithPath << endl;
- studentInfoFile << "Name: " <<name << endl;
- studentInfoFile << "Age: " << age << endl;
- studentInfoFile << "SSN: " << ssn << endl;
- studentInfoFile << "Sex: " << sex << endl;
- studentInfoFile << "Transfer Hours: " << setw(3) << transferHours;
- studentInfoFile << "\tTransfer Quality Points: " << setw(3) << right <<transferQualityPoints << endl;
- studentInfoFile << "MSU Hours: " << right <<setw(3) << msuHours;
- studentInfoFile << "\tMSU Quality Points: " << right << setw(3) <<msuQualityPoints << endl<< endl;
- studentInfoFile << "Transfer GPA: "<<fixed<<setw(5)<<setprecision(2)<< right << tGPA << endl;
- studentInfoFile << "MSU GPA: "<< fixed<<setw(5)<<setprecision(2)<< msuGPA << endl;
- studentInfoFile << "Total GPA: "<< fixed<<setw(5)<<setprecision(2)<< combinedGPA << endl;
- };
- string getFileName(bool useDefaultFileName){
- string fileName, fileNameTemp;
- if (!useDefaultFileName){
- printf("\n Relative to the Current Directory of Program,\n"
- " Please Enter Your File Name (xxxx.txt): ");
- getline(cin, fileName);
- }else fileName = defaultFile;
- fileNameTemp = filePath + fileName;
- return fileNameTemp;
- };
- };
- int main(){
- unsigned short int switchChoice, i=1;
- while (i != 0 ){
- printf("\n\n\t\t\tHomework Number 7\n\n"
- "\t1) Enter New Student\n"
- "\t2) Get Default Student File from info.txt\n"
- "\t3) Enter Student File Name\n\n"
- "\t5) Exit Program\n"
- "\n\t\t\tChoice: ");
- cin >> switchChoice;
- cin.ignore();
- Student studentData;
- switch (switchChoice){
- case 1:
- studentData.getStudent();
- studentData.printStudent();
- studentData.printStudentToFile(1);
- break;
- case 2:
- studentData.getStudentFromFile(studentData.getFileName(1));
- studentData.printStudent();
- studentData.printStudentToFile(0);
- break;
- case 3:
- if(studentData.getStudentFromFile(studentData.getFileName(0)))
- studentData.printStudent();
- studentData.printStudentToFile(0);
- break;
- case 5:
- return 0;
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement