Advertisement
Guest User

C++ Array Project

a guest
Feb 18th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.94 KB | None | 0 0
  1. #include<iomanip>
  2. #include<iostream>
  3. #include<fstream>
  4. #include<cstdlib>
  5. #include<cctype>
  6.  
  7. using namespace std;
  8. const int max_students = 7;
  9.  
  10. class Student
  11. {
  12. private:
  13.     char studentname[30];
  14.     int projgrade[6];
  15.     int examgrade[2];
  16.     int quizgrade[9];
  17.     int finalgrade;
  18.     double projavg;
  19.     double examavg;
  20.     double quizavg;
  21.     double totalavg;
  22.     char grade[30];
  23. public:
  24.     Student();
  25.     ~Student();
  26.    
  27.     void get_student_scores();
  28.     void compute_student_stats();
  29.     void determine_student_grade();
  30.     void display_student_name_gpa_grade();
  31.     double student_average();
  32. };
  33.  
  34. class Course
  35. {
  36. private:
  37.     Student student[max_students];
  38.     int highest;
  39.     int lowest;
  40. public:
  41.     Course();
  42.     ~Course();
  43.    
  44.     void get_grades();
  45.     void evaluate_class();
  46.     void determine_index_of_highest_lowest();
  47.     void display_highest();
  48.     void display_lowest();
  49.     void display_stats();
  50. };
  51.  
  52.     const double projectweight = 0.25,
  53.                  quizweight = 0.10,
  54.                  examweight = 0.15,
  55.                  finalweight = 0.20;
  56.  
  57.  
  58. int main()
  59. {
  60.     Course the_course;
  61.    
  62.     void describe_program();
  63.    
  64.     describe_program();
  65.    
  66.     the_course.get_grades();
  67.     the_course.evaluate_class();
  68.     the_course.determine_index_of_highest_lowest();
  69.     the_course.display_stats();
  70.     the_course.display_highest();
  71.     the_course.display_lowest();
  72.    
  73.    system("PAUSE");
  74.    return 0;
  75. }
  76.  
  77. void describe_program()
  78. {
  79. cout<<"This program will prompt for the names of seven students, and for each \n";
  80. cout<<"student, prompt for the various grades that student has received. \n";
  81. cout<<"The program will also calculate averages for each grade, as well as \n";
  82. cout<<"the student with the highest and student with the lowest class average. \n";
  83. cout<<"All results will be printed to the screen and sent to an external text \n";
  84. cout<<"file, 'grades.txt'.";
  85. cout<<endl<<endl;
  86. }
  87.  
  88.  
  89.  
  90.  
  91. ///////////////////////////////////////////////////////////////////////////////
  92. //                          "Student" Functions                              //
  93. ///////////////////////////////////////////////////////////////////////////////
  94.  
  95. Student::Student()
  96. {
  97.     studentname[30] = '\0';
  98.     projgrade[0] = 0;
  99.     examgrade[0] = 0;
  100.     quizgrade[0] = 0;
  101.     finalgrade = 0;
  102.     projavg = 0;
  103.     examavg = 0;
  104.     quizavg = 0;
  105.     totalavg = 0;
  106.     grade[30] = '\0';
  107. }
  108.  
  109. Student::~Student()
  110. {
  111.    cout<<"Student object going out of existence!\n";
  112.    system("PAUSE");
  113. }
  114.  
  115. void Student::get_student_scores() //Enter grades for the student
  116. {
  117.      int i;
  118.        
  119.      cout<<"Enter the name of the student: ";
  120.      cin.getline(studentname,30);
  121.      cout<<endl<<endl;
  122.      
  123.      for(i=0; i<7; i++) //Enter project grades
  124.         {
  125.         cout<<"Enter the grade for Project "<<i+1<<":";
  126.         cin>>projgrade[i];
  127.         cout<<endl;
  128.         while(projgrade[i] > 100 || projgrade[i] < 0)
  129.          {
  130.           cout<<"Illegal grade entered!"<<endl;
  131.           cout<<"Enter the grade for Project "<<i+1<<":";
  132.           cin>>projgrade[i];
  133.           cout<<endl;
  134.          };
  135.         }
  136.         cout<<endl;
  137.        
  138.      for(i=0; i<3; i++) //Enter exam grades
  139.         {
  140.         cout<<"Enter the grade for Exam "<<i+1<<":";
  141.         cin>>examgrade[i];
  142.         cout<<endl;
  143.        
  144.         while(examgrade[i] > 100 || examgrade[i] < 0)
  145.         {
  146.           cout<<"Illegal grade entered!"<<endl;
  147.           cout<<"Enter the grade for Exam "<<i+1<<":";
  148.           cin>>examgrade[i];
  149.           cout<<endl;
  150.          };
  151.         }
  152.         cout<<endl;
  153.        
  154.      for(i=0; i<10; i++) //Enter quiz grades
  155.         {
  156.         cout<<"Enter the grade for Quiz "<<i+1<<":";
  157.         cin>>quizgrade[i];
  158.         cout<<endl;
  159.        
  160.         while(quizgrade[i] > 100 || quizgrade[i] < 0)
  161.          {
  162.           cout<<"Illegal grade entered!"<<endl;
  163.           cout<<"Enter the grade for Quiz "<<i+1<<":";
  164.           cin>>quizgrade[i];
  165.           cout<<endl;
  166.          };
  167.         }
  168.         cout<<endl;
  169.        
  170.         cout<<"Enter the grade for the Final Exam:";
  171.         cin>>finalgrade;
  172.         while(finalgrade > 100 || finalgrade < 0)
  173.          {
  174.           cout<<"Illegal grade entered!"<<endl;
  175.           cout<<"Enter the grade for Final Exam:";
  176.           cin>>finalgrade;
  177.           cout<<endl;
  178.          };
  179.         cout<<endl<<endl;
  180.  
  181. }
  182.  
  183. void Student::compute_student_stats() //Calculates averages
  184. {
  185.      int i, sum;
  186.      double temp;
  187.      
  188.        //Calculate project average
  189.  
  190.        sum = 0;
  191.        for(i=0; i<6;i++)
  192.           sum += projgrade[i];
  193.        temp = static_cast<double>(sum)/6;
  194.        projavg = static_cast<int>(temp + 0.5);
  195.        
  196.        //Calculate exam average
  197.  
  198.        sum = 0;
  199.        for(i=0;i<3;i++)
  200.           sum += examgrade[i];
  201.        temp = static_cast<double>(sum)/2;
  202.        examavg = static_cast<int>(temp + 0.5);
  203.  
  204.        //Calculate quiz average
  205.  
  206.        sum = 0;
  207.        for(i=0;i<10;i++)
  208.           sum += quizgrade[i];
  209.        temp = static_cast<double>(sum)/5;
  210.        quizavg = static_cast<int>(temp + 0.5);
  211.  
  212. }
  213.  
  214. void Student::determine_student_grade() //Determines total grade and letter grade
  215. {
  216.      
  217.      totalavg = projavg * projectweight +
  218.                 quizavg    * quizweight +  
  219.                 examavg    * examweight +
  220.                 finalgrade * finalweight;
  221.    
  222.        grade[0] = ' ';
  223.        grade[1] = ' ';
  224.        if(totalavg>=93)
  225.        {
  226.             grade[0] = 'A';
  227.             grade[1] = ' ';
  228.        }
  229.        else if(totalavg>=89)
  230.        {
  231.             grade[0] = 'A';
  232.             grade[1] = '-';
  233.        }
  234.        else if(totalavg>=87)
  235.        {
  236.             grade[0] = 'B';
  237.             grade[1] = '+';
  238.        }
  239.        else if(totalavg>=83)
  240.        {
  241.             grade[0] ='B';
  242.             grade[1] = ' ';
  243.        }
  244.        else if(totalavg>=79)
  245.        {
  246.             grade[0] = 'B';
  247.             grade[1] = '-';
  248.        }
  249.        else if(totalavg>=77)
  250.        {
  251.             grade[0] = 'C';
  252.             grade[1] = '+';
  253.        }
  254.        else if(totalavg>=73)
  255.        {
  256.             grade[0] = 'C';
  257.             grade[1] = ' ';
  258.        }
  259.        else if(totalavg>=69)
  260.        {
  261.             grade[0] = 'C';
  262.             grade[1] = '-';
  263.        }
  264.        else if(totalavg>=67)
  265.       {
  266.             grade[0] = 'D';
  267.             grade[1] = '+';
  268.       }
  269.       else if(totalavg>=63)
  270.       {
  271.             grade[0] = 'D';
  272.             grade[1] = ' ';
  273.       }
  274.       else if(totalavg>=57)
  275.       {
  276.             grade[0] = 'D';
  277.             grade[1] = '-';
  278.       }
  279.     else
  280.      {
  281.            grade[0] = 'F';
  282.            grade[1] = ' ';
  283.      }
  284.  
  285. }
  286.  
  287. void Student::display_student_name_gpa_grade() //Display results to screen and text file
  288. {
  289.      cout<<"Student name: "<<studentname<<endl;
  290.      cout<<"Semester Average: "<<totalavg<<endl;
  291.      cout<<"Grade: "<<grade[0]<<grade[1]<<endl;
  292.      
  293.      //Send to text file
  294.      
  295.      ofstream outs;
  296.      outs.open("grades.txt",ios::app);
  297.      
  298.      outs<<"Student name: "<<studentname<<endl;
  299.      outs<<"Semester Average: "<<totalavg<<endl;
  300.      outs<<"Grade: "<<grade<<endl;
  301.      
  302.      outs.close();
  303.  
  304. }
  305.  
  306. double Student::student_average() //Returns average grade for one student
  307. {
  308.  return totalavg;
  309. }
  310.  
  311.  
  312.  
  313. ///////////////////////////////////////////////////////////////////////////////
  314. //                          "Course" Functions                               //
  315. ///////////////////////////////////////////////////////////////////////////////
  316.  
  317.  
  318. Course::Course() //Initialize highest grade and lowest grade subscripts
  319. {
  320.  highest = 0;
  321.  lowest = 0;
  322. }
  323.  
  324. Course::~Course()
  325. {
  326.    cout<<"Course object going out of existence!\n";
  327.    system("PAUSE");
  328. }
  329.  
  330. void Course::get_grades() //Tells user what student # to enter data for
  331. {
  332.      int i;
  333.      
  334.      for(i=0; i<7; i++)
  335.      {
  336.      cout<<"Enter data for student #"<<i+1<<endl;
  337.      student[i].get_student_scores();
  338.      cin.ignore();  
  339.      }  
  340. }
  341.  
  342. void Course::evaluate_class() //Calculates grades for all students
  343. {
  344.      int i;
  345.      
  346.      for(i=0; i<7; i++)
  347.      {
  348.      student[i].compute_student_stats();
  349.      student[i].determine_student_grade();
  350.      }                
  351. }
  352.  
  353. void Course::determine_index_of_highest_lowest() //Finds highest and lowest grade in class.
  354. {
  355.     double max;
  356.     double tempgrade;
  357.     max = 0;
  358.     tempgrade = 0;
  359.    
  360.     for(int i=0; i<7; i++)
  361.     {
  362.           tempgrade = student[i].student_average();
  363.           if(tempgrade>max)
  364.           {
  365.           highest = i;
  366.           max = tempgrade;
  367.           }
  368.     }
  369.    
  370.     max = 101;
  371.     tempgrade = 0;
  372.    
  373.     for(int i=0; i<7; i++)
  374.     {
  375.           tempgrade = student[i].student_average();
  376.           if(tempgrade<max)
  377.           {
  378.           lowest = i;
  379.           max = tempgrade;
  380.           }
  381.     }
  382. }
  383.  
  384. void Course::display_stats() //Displays all student info on screen
  385. {
  386.      int i;
  387.      
  388.    for(i=0; i<7; i++)
  389.    student[i].display_student_name_gpa_grade();
  390.    
  391. }
  392.  
  393. void Course::display_highest() //Displays student with highest grade
  394. {
  395.      ofstream outs;
  396.      outs.open("grades.txt",ios::app);
  397.  
  398.      cout<<"\n\nThe highest grade in the class was: \n";
  399.      outs<<"\n\nThe highest grade in the class was: \n";
  400.      
  401.      outs.close();
  402.      
  403.      student[highest].display_student_name_gpa_grade();
  404. }
  405.  
  406. void Course::display_lowest() //Displays student with lowest grade
  407. {
  408.      ofstream outs;
  409.      outs.open("grades.txt",ios::app);
  410.      
  411.      cout<<"\n\nThe lowest grade in the class was: \n";
  412.      outs<<"\n\nThe lowest grade in the class was: \n";
  413.      
  414.      outs.close();
  415.      
  416.      student[lowest].display_student_name_gpa_grade();                
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement