Guest User

Untitled

a guest
Apr 24th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.41 KB | None | 0 0
  1. /* Hugo Cedano
  2.    finalProject
  3.    part 0
  4. */
  5. #include <iostream>
  6. #include <string>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. struct student{
  12.     string name;
  13.     char sex;
  14.     float gpa;
  15.     char finalGrade;
  16. };
  17.  
  18. const int MAX_IN_CLASS = 20;
  19. student COP1334[MAX_IN_CLASS];
  20.  
  21. bool empty = true;
  22. string searched_Name;
  23.  
  24. void studentReg (student COP1334[], int& enrolled);
  25. void displayStudents (student COP1334[], int& enrolled);
  26. void checkRegistered (student COP1334[], int& enrolled);
  27. void dropStudent (student COP1334[], int& enrolled, int i);
  28. void assignFinalGrade(student COP1334[], int& enrolled);
  29. //void checkArray (student COP1334[], int& enrolled, int i);
  30. void sortName(student COP1334[], int& enrolled);
  31. void sortGPA(student COP1334[], int& enrolled);
  32. void swap(student COP1334[], int i, int j);
  33.  
  34. int main() {
  35.  
  36.     char user_Input,
  37.         sortInput;
  38.     int enrolled = 0,
  39.         assignCount=0;
  40.     string temp,
  41.         tempSort;
  42.    
  43.  
  44.     do{
  45.         cout << "****************************************************" << endl;
  46.         cout << "*" << "Welcome to the COP1334 Student Registration System!" << endl;
  47.         cout << "*" << "To register a student enter A." << endl;
  48.         cout << "*" << "To print all registered students enter P." << endl;
  49.         cout << "*" << "To drop a student enter D." << endl;
  50.         cout << "*" << "To sort students enter S." << endl;
  51.         cout << "*" << "To assign final grades for students enter F." << endl;
  52.         cout << "*" << "To quit this program enter Q." << endl;
  53.         cout << "****************************************************" << endl;
  54.  
  55.         getline(cin, temp);
  56.         if(cin.fail()){
  57.             cin.clear();
  58.             cin.ignore(1337, '\n');
  59.         }
  60.         user_Input = temp[0];
  61.         switch(user_Input){
  62.         case 'A':
  63.         case 'a':
  64.             if (enrolled==MAX_IN_CLASS){
  65.                 cout << "----------------------------------------------------" << endl;
  66.                 cout << "The class is full! Sorry" << endl;
  67.                 cout << "----------------------------------------------------" << endl;
  68.             }
  69.             else
  70.                 studentReg (COP1334, enrolled);
  71.             break;
  72.         case 'P':
  73.         case 'p':
  74.             if (empty){
  75.                 cout << "----------------------------------------------------" << endl;
  76.                 cout << "There are no registered students" << endl;
  77.                 cout << "----------------------------------------------------" << endl;
  78.             }
  79.             else{
  80.                 displayStudents (COP1334, enrolled);
  81.             }
  82.             break;
  83.         case 'D':
  84.         case 'd':
  85.             if (empty){
  86.                 cout << "----------------------------------------------------" << endl;
  87.                 cout << "There are no students to drop!" << endl;
  88.                 cout << "----------------------------------------------------" << endl;
  89.             }
  90.             else
  91.                 checkRegistered (COP1334, enrolled);
  92.             break;
  93.         case 'F':
  94.         case 'f':
  95.             if (empty){
  96.                 cout << "----------------------------------------------------" << endl;
  97.                 cout << "There are no students to assign grades to!" << endl;
  98.                 cout << "----------------------------------------------------" << endl;
  99.             }
  100.             else if (enrolled == assignCount){
  101.                 cout << "All grades have been assigned" << endl;
  102.             }
  103.             else{
  104.                 //assignFinalGrade(COP1334,enrolled);
  105.                 for(int i=0; i<enrolled; i++){
  106.                     if(COP1334[i].finalGrade == 'N'){
  107.                         assignFinalGrade(COP1334, enrolled);
  108.                     }
  109.                 }
  110.                 break;
  111.             }
  112.                
  113.             break;
  114.         case 'S':
  115.         case 's':
  116.             if (empty){
  117.                 cout << "----------------------------------------------------" << endl;
  118.                 cout << "There are no students to sort!" << endl;
  119.                 cout << "----------------------------------------------------" << endl;
  120.             }
  121.             else{
  122.                 cout << "----------------------------------------------------" << endl;
  123.                 cout << "To sort students by their Name enter N." << endl;
  124.                 cout << "To sort students by their GPA enter G." << endl;
  125.                 cout << "----------------------------------------------------" << endl;
  126.                 getline(cin, tempSort);
  127.                 if(cin.fail()){
  128.                     cin.clear();
  129.                     cin.ignore(1337, '\n');
  130.                 }
  131.                 sortInput = tempSort[0];
  132.                 switch (sortInput){
  133.                 case 'N':
  134.                 case 'n':
  135.                     sortName(COP1334, enrolled);
  136.                     break;
  137.                 case 'G':
  138.                 case 'g':
  139.                     sortGPA(COP1334, enrolled);
  140.                     break;
  141.                 default:
  142.                     cout << "Invalid input" << endl;
  143.                 }
  144.  
  145.             }
  146.             break;
  147.         case 'Q':
  148.         case 'q':
  149.             break;
  150.         default:
  151.             cout << "Invalid input" << endl;
  152.         }
  153.     }
  154.     while (user_Input != 'Q');
  155.  
  156.     return 0;
  157. }
  158.  
  159. void studentReg (student COP1334[], int& enrolled){
  160.     student s;
  161.     string temp;
  162.     cout << "Enter Name: ";
  163.     getline(cin, s.name);
  164.     cout << "GPA: ";
  165.     cin >> s.gpa;
  166.     cout << "Sex: ";
  167.     cin.ignore(1, '\n');
  168.     getline(cin,temp);
  169.     s.sex = temp.substr(0,1)[0];
  170.     s.finalGrade = 'N';
  171.  
  172.     COP1334[enrolled] = s;
  173.     enrolled++;
  174.     empty = false;
  175.     cout << "Student successfully registered" << endl;
  176. }
  177.  
  178. void displayStudents (student COP1334[], int& enrolled){
  179.     cout << "----------------------------------------------------" << endl;
  180.     cout << "Class Capacity: " << enrolled << '/' << MAX_IN_CLASS << "         N- \"Not Entered\"\n";
  181.     for(int i=0; i<enrolled; i++){
  182.         cout << "----------------------------------------------------" << endl;
  183.         cout << "Name\tGPA\tGender\tFinal Grade" << endl;
  184.         cout << COP1334[i].name << "\t" << COP1334[i].gpa << "\t" << COP1334[i].sex << "\t" << COP1334[i].finalGrade << endl;
  185.         cout << "----------------------------------------------------" << endl;
  186.     }
  187. }
  188.  
  189. void checkRegistered (student COP1334[], int& enrolled){
  190.     bool studentFound=false;
  191.     cout << "----------------------------------------------------" << endl;
  192.     cout << "Enter name of student to drop: " << endl;
  193.     cout << "----------------------------------------------------" << endl;
  194.     getline(cin, searched_Name);
  195.    
  196.     if(searched_Name.compare(COP1334[enrolled-1].name) == 0){
  197.         enrolled--;
  198.         cout << "----------------------------------------------------" << endl;
  199.         cout << "Student was dropped successfully" << endl;
  200.         cout << "----------------------------------------------------" << endl;
  201.         if(enrolled==0){
  202.             empty=true;
  203.         }
  204.         return;
  205.     }
  206.     //check if student is registered and calls the actual drop student function
  207.     for(int i=0; i<enrolled-1; i++){
  208.         if(searched_Name==(COP1334[i].name)){
  209.             studentFound = true;
  210.             dropStudent (COP1334, enrolled, i);
  211.             cout << "----------------------------------------------------" << endl;
  212.             cout << "Student was dropped successfully!" << endl;
  213.             cout << "----------------------------------------------------" << endl;
  214.         }
  215.     }
  216.     if(!(studentFound)){
  217.         cout << "----------------------------------------------------" << endl;
  218.         cout << "Student isn't registered at this time" << endl;
  219.         cout << "----------------------------------------------------" << endl;
  220.     }
  221.     if(enrolled==0){
  222.         empty=true;
  223.     }
  224. }  
  225.  
  226. void assignFinalGrade(student COP1334[], int& enrolled, int& assignCount){
  227.     char user_Input, user_Input2, user_Input3;
  228.     string temp, temp2, temp3;
  229.    
  230.     for (int i=0; i<enrolled; i++){
  231.         cout << "----------------------------------------------------" << endl;
  232.         cout << "To assign a final grade for " << COP1334[i].name << ", press A" << endl;
  233.         cout << "To proceed to the next student, press P" << endl;
  234.         cout << "----------------------------------------------------" << endl;
  235.         getline(cin, temp);
  236.         if(cin.fail()){
  237.             cin.clear();
  238.             cin.ignore(1337, '\n');
  239.         }
  240.         user_Input = temp[0];
  241.         switch (user_Input){
  242.         case 'A':
  243.         case 'a':
  244.             do{
  245.                 cout << "----------------------------------------------------" << endl;
  246.                 cout << "Enter a grade to assign to " << COP1334[i].name << ": " << endl;
  247.                 cout << "----------------------------------------------------" << endl;
  248.                 getline(cin, temp2);
  249.                 if(cin.fail()){
  250.                     cin.clear();
  251.                     cin.ignore(1337, '\n');
  252.                 }
  253.                 user_Input2 = temp2[0];
  254.                 cout << "----------------------------------------------------" << endl;
  255.                 cout << "You are about to assign " << user_Input2 << " to " << COP1334[i].name << endl;
  256.                 cout << "Enter P to proceed or C to cancel: " << endl;
  257.                 cout << "----------------------------------------------------" << endl;
  258.                 getline(cin, temp3);
  259.                 user_Input3 = temp3[0];
  260.                 switch (user_Input3){
  261.                 case 'p':
  262.                 case 'P':
  263.                     COP1334[i].finalGrade = user_Input2;
  264.                     assignCount++;
  265.                     break;
  266.                 case 'c':
  267.                 case 'C':
  268.                     break;
  269.                 default:
  270.                     cout << "Invalid input!" << endl;
  271.                 }
  272.            
  273.                 break;
  274.         case'p':
  275.         case'P':
  276.             //implement if...else code
  277.             if (enrolled=1){
  278.                 break;
  279.             }
  280.             else
  281.                 if(assignCount = enrolled){
  282.                     cout << "All final grades are already assigned" << endl;
  283.                     break;
  284.             }
  285.             else{
  286.                  assignFinalGrade;
  287.             }
  288.             break;
  289.         default:
  290.             cout << "Invalid input!" << endl;
  291.             }
  292.             while(user_Input2 != 'C');
  293.         }
  294.     }
  295. }
  296.  
  297. void dropStudent (student COP1334[], int& enrolled, int i){
  298.     for(int i=0; i<enrolled; i++){
  299.         if(searched_Name.compare(COP1334[i].name) == 0){
  300.             for(int j=i; j<enrolled-1; j++){
  301.                 COP1334[j] = COP1334[j+1];
  302.             }
  303.             enrolled--;
  304.         }
  305.     }
  306.     if(enrolled == 0){
  307.         empty = true;
  308.     }
  309. }
  310.  
  311. int binarySearch(int COP1334[], int& enrolled, int value)
  312. {
  313.     int first = 0,
  314.         last = enrolled - 1,
  315.         middle,
  316.         position = -1;
  317.     bool found = false;
  318.  
  319.     while (!found && first <= last)
  320.     {
  321.         middle = (first + last)/2;
  322.         if (COP1334[middle] == value)
  323.         {
  324.             found = true;
  325.             position = middle;
  326.         }
  327.         else if (COP1334[middle] > value)
  328.             last = middle - 1;
  329.         else
  330.             first = middle + 1;
  331.     }
  332.     return position;
  333. }
  334. void sortName(student COP1334[], int& enrolled){
  335.     for(int i=0; i<enrolled-1; i++){
  336.         bool sorted = false;
  337.         for(int j=1; j<=enrolled-1; j++){
  338.             if(COP1334[j-1].name > COP1334[j].name){
  339.                 cout << "a at "<< j << " is: " << COP1334[j].name <<endl;
  340.                 cout << "a at "<< j-1 <<" is: " << COP1334[j-1].name <<endl;
  341.                 swap(COP1334, j, j-1);
  342.                 sorted = true;
  343.             }
  344.         }
  345.         if(!sorted){
  346.             break;
  347.         }
  348.     }
  349. }
  350. void sortGPA(student COP1334[], int& enrolled){
  351.     for(int i=0; i<enrolled-1; i++){
  352.         bool sorted = false;
  353.         for(int j=1; j<=enrolled-1; j++){
  354.             if(COP1334[j-1].gpa > COP1334[j].gpa){
  355.                 cout << "a at "<< j << " is: " << COP1334[j].gpa <<endl;
  356.                 cout << "a at "<< j-1 <<" is: " << COP1334[j-1].gpa <<endl;
  357.                 swap(COP1334, j, j-1);
  358.                 sorted = true;
  359.             }
  360.         }
  361.         if(!sorted){
  362.             break;
  363.         }
  364.     }
  365. }
  366. void swap(student COP1334[], int x, int y){
  367.     student temp;
  368.     temp = COP1334[x];
  369.     COP1334[x] = COP1334[y];
  370.     COP1334[y] = temp;
  371. }
  372. /*void checkArray (student COP1334[], int& enrolled, int i, int& assignCount){
  373.     for(int i=0; i<enrolled; i++){
  374.         if(COP1334[i].finalGrade == 'N'){
  375.             assignFinalGrade(COP1334, enrolled, assignCount);
  376.         }
  377.         else
  378.             break;
  379.     }
  380. }*/
Add Comment
Please, Sign In to add comment