Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.19 KB | None | 0 0
  1. // assign_problem5.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4.  
  5. #include <iostream>
  6. #include <conio.h>
  7. #include <string>
  8.  
  9. using namespace System;
  10. using namespace std;
  11.  
  12. //Prototypes
  13. //Sets up the screen for entering data
  14. void setScreenData(void);
  15. //Sets up the screen for displaying stats
  16. void setScreenStats(void);
  17. //Checks for invalid marks
  18. void invalidCheck(int & mark, int row);
  19. //Calculates the student's result
  20. void calcResult(int cwMark, int exMark, int & mark, string & grade, int & numStudents,
  21.                 int & cwSum, int & exSum, int & markSum, int gradeCount[]);
  22. //Calculates the averages
  23. void calcAvg(double & avgMark, double & cwAvg, double & exAvg, string & avgGrade,
  24.              int numStudents, int cwSum, int exSum, int markSum);
  25.  
  26. int main(array<System::String ^> ^args)
  27. {
  28.     //Declare variables
  29.     string name, grade, avgGrade;
  30.     int x, cwMark, exMark, mark, numStudents = 0, markSum = 0, cwSum = 0, exSum = 0, gradeCount[5] = {0};
  31.     double avgMark = 0.0, cwAvg = 0.0, exAvg = 0.0;
  32.     char again;
  33.  
  34.     //Allow the user to enter student marks as many times they want
  35.     do
  36.     {
  37.         //Set up the screen
  38.         setScreenData();
  39.  
  40.         //Get the student name from the user
  41.         Console::SetCursorPosition(51, 4);
  42.         getline(cin, name);
  43.  
  44.         //Get the coursework mark from the user
  45.         Console::SetCursorPosition(51, 6);
  46.         cin >> cwMark;
  47.         //Remove the last character (\n) from istream to avoid erros with getline
  48.         cin.ignore();
  49.  
  50.         //Check if the user entered an invalid number
  51.         invalidCheck(cwMark, 6);
  52.  
  53.         //Get the examination mark from the user
  54.         Console::SetCursorPosition(51, 7);
  55.         cin >> exMark;
  56.         //Remove the last character (\n) from istream to avoid erros with getline
  57.         cin.ignore();
  58.  
  59.         //Check if the user entered an invalid number
  60.         invalidCheck(exMark, 7);
  61.  
  62.         //Calculate the student's result
  63.         calcResult(cwMark, exMark, mark, grade, numStudents, cwSum, exSum, markSum, gradeCount);
  64.  
  65.         //Output the results
  66.         Console::SetCursorPosition(51, 9); cout << mark;
  67.         Console::SetCursorPosition(51, 10); cout << grade;
  68.  
  69.         //Ask the user if they would like to move on to the next student
  70.         Console::SetCursorPosition(20, 24);
  71.         cout << "Next student? (Y)es / (N)o [_]";
  72.  
  73.         //Get the user's reply
  74.         Console::SetCursorPosition(48, 24);
  75.         again = getche();
  76.  
  77.         //Convert again to lowercase
  78.         again = tolower(again);
  79.  
  80.         //Show an error message and get another letter from the user if they enetered
  81.         //an invalid option
  82.         while(again != 'y' && again != 'n')
  83.         {
  84.             //Show an error message
  85.             Console::SetCursorPosition(60, 24);
  86.             cout << "Invalid option!";
  87.  
  88.             //Remove the letter the user entered
  89.             Console::SetCursorPosition(48, 24);
  90.             cout << " ]";
  91.  
  92.             //Get another letter from the user
  93.             Console::SetCursorPosition(48, 24);
  94.             again = getche();
  95.  
  96.             //Convert again to lowercase
  97.             again = tolower(again);
  98.  
  99.             //Remove the error message
  100.             Console::SetCursorPosition(60, 24);
  101.             cout << "               ";
  102.         }
  103.     }while(again != 'n');
  104.  
  105.     //Set up the screen for showing statistics
  106.     setScreenStats();
  107.  
  108.     //Calculate the averages
  109.     calcAvg(avgMark, cwAvg, exAvg, avgGrade, numStudents, cwSum, exSum, markSum);
  110.  
  111.     //Output the grade counts
  112.     for(x = 0; x < 5; x++)
  113.     {
  114.         Console::SetCursorPosition(41, x + 6);
  115.         cout << gradeCount[x];
  116.     }
  117.  
  118.     //Output the averages
  119.     Console::SetCursorPosition(41, 12); cout << avgMark;
  120.     Console::SetCursorPosition(41, 13); cout << avgGrade;
  121.  
  122.     Console::SetCursorPosition(41, 15); cout << cwAvg;
  123.     Console::SetCursorPosition(41, 16); cout << exAvg;
  124.  
  125.     //Wait for a key press before exiting
  126.     Console::SetCursorPosition(79, 24);
  127.     getch();
  128. }
  129.  
  130. void setScreenData(void)
  131. {
  132.     Console::Clear();
  133.  
  134.     Console::SetCursorPosition(30, 2); cout << "Computer Science grades";
  135.  
  136.     Console::SetCursorPosition(20, 4); cout << "Student name:";
  137.  
  138.     Console::SetCursorPosition(20, 6); cout << "Coursework mark:";
  139.     Console::SetCursorPosition(20, 7); cout << "Examination mark:";
  140.  
  141.     Console::SetCursorPosition(20, 9); cout << "Mark:";
  142.     Console::SetCursorPosition(20, 10); cout << "Grade:";
  143.  
  144.     Console::SetCursorPosition(50, 4); cout << "[                          ]";
  145.  
  146.     Console::SetCursorPosition(50, 6); cout << "[  ]";
  147.     Console::SetCursorPosition(50, 7); cout << "[  ]";
  148.  
  149.     Console::SetCursorPosition(50, 9); cout << "[  ]";
  150.     Console::SetCursorPosition(50, 10); cout << "[  ]";
  151. }
  152.  
  153. void setScreenStats(void)
  154. {
  155.     Console::Clear();
  156.  
  157.     Console::SetCursorPosition(30, 2); cout << "Class statistics";
  158.  
  159.     Console::SetCursorPosition(10, 4); cout << "Number of students who achieved: ";
  160.  
  161.     Console::SetCursorPosition(10, 6); cout << "Grade A:";
  162.     Console::SetCursorPosition(10, 7); cout << "Grade B+:";
  163.     Console::SetCursorPosition(10, 8); cout << "Grade B:";
  164.     Console::SetCursorPosition(10, 9); cout << "Grade C:";
  165.     Console::SetCursorPosition(10, 10); cout << "Grade F:";
  166.  
  167.     Console::SetCursorPosition(10, 12); cout << "Class average mark:";
  168.     Console::SetCursorPosition(10, 13); cout << "Class average grade:";
  169.  
  170.     Console::SetCursorPosition(10, 15); cout << "Average coursework mark:";
  171.     Console::SetCursorPosition(10, 16); cout << "Average examination mark:";
  172.  
  173.     Console::SetCursorPosition(40, 6); cout << "[  ]";
  174.     Console::SetCursorPosition(40, 7); cout << "[  ]";
  175.     Console::SetCursorPosition(40, 8); cout << "[  ]";
  176.     Console::SetCursorPosition(40, 9); cout << "[  ]";
  177.     Console::SetCursorPosition(40, 10); cout << "[  ]";
  178.  
  179.     Console::SetCursorPosition(40, 12); cout << "[    ]";
  180.     Console::SetCursorPosition(40, 13); cout << "[  ]";
  181.  
  182.     Console::SetCursorPosition(40, 15); cout << "[    ]";
  183.     Console::SetCursorPosition(40, 16); cout << "[    ]";
  184.  
  185.     Console::SetCursorPosition(30, 24);
  186.     cout << "Press any key to exit...";
  187. }
  188.  
  189. void invalidCheck(int & mark, int row)
  190. {
  191.     //If the user enters an invalid number, show an error message and
  192.     //get another number from them
  193.     while(mark < 0 || mark > 50)
  194.     {
  195.         //Show an error message
  196.         Console::SetCursorPosition(60, row);
  197.         cout << "Invalid number!";
  198.  
  199.         //Remove the number the user entered
  200.         Console::SetCursorPosition(51, row);
  201.         cout << "  ]";
  202.  
  203.         //Get another number from the user
  204.         Console::SetCursorPosition(51, row);
  205.         cin >> mark;
  206.  
  207.         //Remove the error message
  208.         Console::SetCursorPosition(60, row);
  209.         cout << "               ";
  210.     }
  211. }
  212.  
  213. void calcResult(int cwMark, int exMark, int & mark, string & grade, int & numStudents,
  214.                 int & cwSum, int & exSum, int & markSum, int gradeCount[])
  215. {
  216.     //Calculate the total mark for the student
  217.     mark = cwMark + exMark;
  218.  
  219.     //If the student scored lower than 40 but scored 15 in each component
  220.     //allow them to pass
  221.     if(mark < 40 && cwMark >= 15 && exMark >= 15)
  222.         mark = 40;
  223.     else
  224.         //If the student scored 40 or more but did not score at least 15
  225.         //in each component, fail them
  226.         if(mark >= 40 && (cwMark < 15 || exMark < 15))
  227.             mark = 39;
  228.  
  229.     //Calculate the grade
  230.     if(mark >= 70)
  231.     {
  232.         grade = "A";
  233.  
  234.         //Increase the number of A's
  235.         gradeCount[0]++;
  236.     }
  237.     else
  238.     {
  239.         if(mark >= 60)
  240.         {
  241.             grade = "B+";
  242.  
  243.             //Increase the number of B+'s
  244.             gradeCount[1]++;
  245.         }
  246.         else
  247.         {
  248.             if(mark >= 50)
  249.             {
  250.                 grade = "B";
  251.  
  252.                 //Increase the number of B's
  253.                 gradeCount[2]++;
  254.             }
  255.             else
  256.             {
  257.                 if(mark >= 40)
  258.                 {
  259.                     grade = "C";
  260.  
  261.                     //Increase the number of C's
  262.                     gradeCount[3]++;
  263.                 }
  264.                 else
  265.                 {
  266.                     grade = "F";
  267.  
  268.                     //Increase the number of F's
  269.                     gradeCount[4]++;
  270.                 }
  271.             }
  272.         }
  273.     }
  274.  
  275.     //Add cwMark to cwSum
  276.     cwSum += cwMark;
  277.  
  278.     //Add exMark to exSum
  279.     exSum += exMark;
  280.  
  281.     //Add mark to markSum
  282.     markSum += mark;
  283.  
  284.     //Increase the number of students
  285.     numStudents++;
  286. }
  287.                
  288. void calcAvg(double & avgMark, double & cwAvg, double & exAvg, string & avgGrade,
  289.              int numStudents, int cwSum, int exSum, int markSum)
  290. {
  291.     //Calculate class average mark
  292.     avgMark = static_cast<double>(markSum) / numStudents;
  293.  
  294.     //Calculate the class average grade
  295.     if(avgMark >= 70)
  296.         avgGrade = "A";
  297.     else
  298.         if(avgMark >= 60)
  299.             avgGrade = "B+";
  300.         else
  301.             if(avgMark >= 50)
  302.                 avgGrade = "B";
  303.             else
  304.                 if(avgMark >= 40)
  305.                     avgGrade = "C";
  306.                 else
  307.                     avgGrade = "F";
  308.  
  309.     //Calculate the average coursework mark
  310.     cwAvg = static_cast<double>(cwSum) / numStudents;
  311.  
  312.     //Calculate the average examination mark
  313.     exAvg = static_cast<double>(exSum) / numStudents;
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement