Advertisement
Guest User

fix'd

a guest
Mar 8th, 2011
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. class Student{
  8.     private:
  9.         string name, ssn, sex, fileWithPath, filePath, defaultFile;
  10.         unsigned short int age, transferHours, transferQualityPoints, msuHours, msuQualityPoints;
  11.         float tGPA, msuGPA, combinedGPA;
  12.     public:
  13.         Student(string name, unsigned short int age, string ssn, string sex,
  14.             unsigned short int transferHours, unsigned short int transferQualityPoints,
  15.             unsigned short int msuHours, unsigned short int msuQualityPoints){
  16.                 Student::name = name;
  17.                 Student::age = age;
  18.                 Student::ssn = ssn;
  19.                 Student::sex = sex;
  20.                 Student::transferHours = transferHours;
  21.                 Student::transferQualityPoints = transferQualityPoints;
  22.                 Student::msuHours = msuHours;
  23.                 Student::msuQualityPoints = msuQualityPoints;
  24.                 Student::fileWithPath = fileWithPath;
  25.                 Student::filePath = filePath;
  26.                 Student::defaultFile = defaultFile;
  27.                 calcGPA();
  28.         };
  29.  
  30.         Student(){
  31.             string studentName, studentSSN, studentSex, fileWithPathString, filePathString;
  32.            
  33.             name = "Leeroy Jenkins";
  34.             ssn= "XXX-xx-XXXX";
  35.             sex= "Sex";
  36.             defaultFile= "info.txt";
  37.             filePath = "/Users/leeroy/Desktop/eerk/";
  38. //          filePath = "C:\\Documents and Settings\\Leeroy\\Desktop\\";
  39.             fileWithPath = defaultFile + filePath;
  40.  
  41.             age = 24;
  42.             transferHours=12;
  43.             transferQualityPoints=47;
  44.             msuHours=48;
  45.             msuQualityPoints=120;
  46.  
  47.             calcGPA();
  48.         }
  49.         ~Student(){};
  50.  
  51.         void getStudent(){
  52.            
  53.             printf("Please Enter First and Last Name: ");
  54.             getline(cin, name);
  55.             printf("Please Enter SSN (123-45-6789): ");
  56.             getline(cin, ssn);
  57.             printf("Please Enter Sex: ");
  58.             getline(cin, sex);
  59.             printf("Please Enter Age: ");
  60.             cin >> age;
  61.             printf("Please Enter Transfer Credit Hours: ");
  62.             cin >> transferHours;
  63.             printf("Please Enter Transfer Quality Points: ");
  64.             cin >> transferQualityPoints;
  65.             printf("Please Enter MSU Credit Hours: ");
  66.             cin >> msuHours;
  67.             printf("Please Enter MSU Quality Points: ");
  68.             cin >> msuQualityPoints;
  69.             cin.ignore();
  70.             calcGPA();
  71.         };
  72.  
  73.         bool getStudentFromFile(string fileName){
  74.                 string firstName,lastName,aLine, aWord;
  75.                
  76.                 ifstream studentInfo(fileName.c_str());
  77.  
  78.                 if (!studentInfo){
  79.                     printf("\n\n\t----Failed to intilize file----\n\t\t  Aborting\n");
  80.                     return 0;
  81.                 }
  82.                 else {
  83.                     studentInfo>>aWord;
  84.                     studentInfo>> firstName>> lastName;
  85.                     name = firstName + " " + lastName;
  86.                     studentInfo>>aWord;
  87.                     studentInfo>> age;
  88.                     studentInfo>>aWord;
  89.                     studentInfo>>ssn;
  90.                     studentInfo>>aWord;
  91.                     studentInfo>>sex;
  92.                     studentInfo>>aWord; studentInfo>>aWord;
  93.                     studentInfo>>transferHours;
  94.                     studentInfo>>aWord; studentInfo>>aWord; studentInfo>>aWord;
  95.                     studentInfo>>transferQualityPoints;
  96.                     studentInfo>>aWord; studentInfo>>aWord;
  97.                     studentInfo>>msuHours;
  98.                     studentInfo>>aWord; studentInfo>>aWord; studentInfo>>aWord;
  99.                     studentInfo>>msuQualityPoints;
  100.                     calcGPA();
  101.  
  102.                     return 1;
  103.                 };
  104.         };
  105.  
  106.         float calcGPA(){
  107.             int combinedCreditHours, combinedQualityPoints;
  108.             tGPA = static_cast<float>(transferQualityPoints)/transferHours;
  109.             msuGPA = static_cast<float>(msuQualityPoints)/msuHours;
  110.             combinedCreditHours= transferHours + msuHours;
  111.             combinedQualityPoints= transferQualityPoints + msuQualityPoints;
  112.             combinedGPA= static_cast<float>(combinedQualityPoints)/combinedCreditHours;
  113.             return 1;
  114.         };
  115.  
  116.         void printStudent(){
  117.             cout << "\n\n *********************************************\n\n"
  118.                 << "Name:   " << name << endl
  119.                 << "Age:    " << age << endl
  120.                 << "SSN:    " << ssn << endl
  121.                 << "Sex:    " << sex << endl << endl
  122.                 << "Transfer Hours:  " << setw(3) << transferHours
  123.                 << "\t Transfer Quality Points: " << setw(3) << right <<transferQualityPoints << endl
  124.                 << "MSU Hours:   " << right <<setw(3) << msuHours
  125.                 << "\t MSU Quality Points: " << right << setw(3) <<msuQualityPoints << endl<< endl
  126.                 << "Transfer GPA:   "<<fixed<<setw(5)<<setprecision(2)<< right << tGPA << endl
  127.                 << "MSU GPA:    "<< fixed<<setw(5)<<setprecision(2)<< msuGPA << endl
  128.                 << "Total GPA:  "<< fixed<<setw(5)<<setprecision(2)<< combinedGPA << endl
  129.                 << "\n *********************************************\n\n";
  130.             cin.ignore();
  131.         };
  132.  
  133.         void printStudentToFile(bool defaultSSN){
  134.            
  135.             if (defaultSSN) fileWithPath = filePath + ssn + ".txt";
  136.             else fileWithPath = filePath + "record.txt";
  137.            
  138.             ofstream studentInfoFile(fileWithPath.c_str());
  139.  
  140.             if (!studentInfoFile) printf("\n\n\t----Failed to create file----\n\t \t Aborting\n");
  141.             else  cout << "Saving to " << fileWithPath << endl;
  142.  
  143.                 studentInfoFile << "Name: " <<name << endl;
  144.                 studentInfoFile << "Age: " << age << endl;
  145.                 studentInfoFile << "SSN: " << ssn << endl;
  146.                 studentInfoFile << "Sex: " << sex << endl;
  147.                 studentInfoFile << "Transfer Hours:  " << setw(3) << transferHours;
  148.                 studentInfoFile << "\tTransfer Quality Points: " << setw(3) << right <<transferQualityPoints << endl;
  149.                 studentInfoFile << "MSU Hours:   " << right <<setw(3) << msuHours;
  150.                 studentInfoFile << "\tMSU Quality Points: " << right << setw(3) <<msuQualityPoints << endl<< endl;
  151.                 studentInfoFile << "Transfer GPA:   "<<fixed<<setw(5)<<setprecision(2)<< right << tGPA << endl;
  152.                 studentInfoFile << "MSU GPA:    "<< fixed<<setw(5)<<setprecision(2)<< msuGPA << endl;
  153.                 studentInfoFile << "Total GPA:  "<< fixed<<setw(5)<<setprecision(2)<< combinedGPA << endl;
  154.         };
  155.        
  156.         string getFileName(bool useDefaultFileName){
  157.             string fileName, fileNameTemp;
  158.            
  159.             if (!useDefaultFileName){
  160.                 printf("\n Relative to the Current Directory of Program,\n"
  161.                     "  Please Enter Your File Name (xxxx.txt): ");
  162.                 getline(cin, fileName);
  163.             }else fileName = defaultFile;
  164.            
  165.             fileNameTemp = filePath + fileName;
  166.            
  167.             return fileNameTemp;
  168.         };
  169. };
  170.  
  171. int main(){
  172.     unsigned short int switchChoice, i=1;
  173.  
  174.     while (i != 0 ){
  175.     printf("\n\n\t\t\tHomework Number 7\n\n"
  176.             "\t1) Enter New Student\n"
  177.             "\t2) Get Default Student File from info.txt\n"
  178.             "\t3) Enter Student File Name\n\n"
  179.             "\t5) Exit Program\n"
  180.             "\n\t\t\tChoice: ");
  181.            
  182.     cin >> switchChoice;
  183.     cin.ignore();
  184.     Student studentData;
  185.  
  186.         switch (switchChoice){
  187.             case 1:
  188.                 studentData.getStudent();
  189.                 studentData.printStudent();
  190.                 studentData.printStudentToFile(1);
  191.                 break;
  192.             case 2:
  193.                 studentData.getStudentFromFile(studentData.getFileName(1));
  194.                 studentData.printStudent();
  195.                 studentData.printStudentToFile(0);
  196.                 break;
  197.             case 3:
  198.                 if(studentData.getStudentFromFile(studentData.getFileName(0)))
  199.                     studentData.printStudent();
  200.                 studentData.printStudentToFile(0);
  201.                 break;
  202.             case 5:
  203.                 return 0;
  204.         }
  205.     };
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement