Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // assign_problem5.cpp : main project file.
- #include "stdafx.h"
- #include <iostream>
- #include <conio.h>
- #include <string>
- using namespace System;
- using namespace std;
- //Prototypes
- //Sets up the screen for entering data
- void setScreenData(void);
- //Sets up the screen for displaying stats
- void setScreenStats(void);
- //Checks for invalid marks
- void invalidCheck(int & mark, int row);
- //Calculates the student's result
- void calcResult(int cwMark, int exMark, int & mark, string & grade, int & numStudents,
- int & cwSum, int & exSum, int & markSum, int gradeCount[]);
- //Calculates the averages
- void calcAvg(double & avgMark, double & cwAvg, double & exAvg, string & avgGrade,
- int numStudents, int cwSum, int exSum, int markSum);
- int main(array<System::String ^> ^args)
- {
- //Declare variables
- string name, grade, avgGrade;
- int x, cwMark, exMark, mark, numStudents = 0, markSum = 0, cwSum = 0, exSum = 0, gradeCount[5] = {0};
- double avgMark = 0.0, cwAvg = 0.0, exAvg = 0.0;
- char again;
- //Allow the user to enter student marks as many times they want
- do
- {
- //Set up the screen
- setScreenData();
- //Get the student name from the user
- Console::SetCursorPosition(51, 4);
- getline(cin, name);
- //Get the coursework mark from the user
- Console::SetCursorPosition(51, 6);
- cin >> cwMark;
- //Remove the last character (\n) from istream to avoid erros with getline
- cin.ignore();
- //Check if the user entered an invalid number
- invalidCheck(cwMark, 6);
- //Get the examination mark from the user
- Console::SetCursorPosition(51, 7);
- cin >> exMark;
- //Remove the last character (\n) from istream to avoid erros with getline
- cin.ignore();
- //Check if the user entered an invalid number
- invalidCheck(exMark, 7);
- //Calculate the student's result
- calcResult(cwMark, exMark, mark, grade, numStudents, cwSum, exSum, markSum, gradeCount);
- //Output the results
- Console::SetCursorPosition(51, 9); cout << mark;
- Console::SetCursorPosition(51, 10); cout << grade;
- //Ask the user if they would like to move on to the next student
- Console::SetCursorPosition(20, 24);
- cout << "Next student? (Y)es / (N)o [_]";
- //Get the user's reply
- Console::SetCursorPosition(48, 24);
- again = getche();
- //Convert again to lowercase
- again = tolower(again);
- //Show an error message and get another letter from the user if they enetered
- //an invalid option
- while(again != 'y' && again != 'n')
- {
- //Show an error message
- Console::SetCursorPosition(60, 24);
- cout << "Invalid option!";
- //Remove the letter the user entered
- Console::SetCursorPosition(48, 24);
- cout << " ]";
- //Get another letter from the user
- Console::SetCursorPosition(48, 24);
- again = getche();
- //Convert again to lowercase
- again = tolower(again);
- //Remove the error message
- Console::SetCursorPosition(60, 24);
- cout << " ";
- }
- }while(again != 'n');
- //Set up the screen for showing statistics
- setScreenStats();
- //Calculate the averages
- calcAvg(avgMark, cwAvg, exAvg, avgGrade, numStudents, cwSum, exSum, markSum);
- //Output the grade counts
- for(x = 0; x < 5; x++)
- {
- Console::SetCursorPosition(41, x + 6);
- cout << gradeCount[x];
- }
- //Output the averages
- Console::SetCursorPosition(41, 12); cout << avgMark;
- Console::SetCursorPosition(41, 13); cout << avgGrade;
- Console::SetCursorPosition(41, 15); cout << cwAvg;
- Console::SetCursorPosition(41, 16); cout << exAvg;
- //Wait for a key press before exiting
- Console::SetCursorPosition(79, 24);
- getch();
- }
- void setScreenData(void)
- {
- Console::Clear();
- Console::SetCursorPosition(30, 2); cout << "Computer Science grades";
- Console::SetCursorPosition(20, 4); cout << "Student name:";
- Console::SetCursorPosition(20, 6); cout << "Coursework mark:";
- Console::SetCursorPosition(20, 7); cout << "Examination mark:";
- Console::SetCursorPosition(20, 9); cout << "Mark:";
- Console::SetCursorPosition(20, 10); cout << "Grade:";
- Console::SetCursorPosition(50, 4); cout << "[ ]";
- Console::SetCursorPosition(50, 6); cout << "[ ]";
- Console::SetCursorPosition(50, 7); cout << "[ ]";
- Console::SetCursorPosition(50, 9); cout << "[ ]";
- Console::SetCursorPosition(50, 10); cout << "[ ]";
- }
- void setScreenStats(void)
- {
- Console::Clear();
- Console::SetCursorPosition(30, 2); cout << "Class statistics";
- Console::SetCursorPosition(10, 4); cout << "Number of students who achieved: ";
- Console::SetCursorPosition(10, 6); cout << "Grade A:";
- Console::SetCursorPosition(10, 7); cout << "Grade B+:";
- Console::SetCursorPosition(10, 8); cout << "Grade B:";
- Console::SetCursorPosition(10, 9); cout << "Grade C:";
- Console::SetCursorPosition(10, 10); cout << "Grade F:";
- Console::SetCursorPosition(10, 12); cout << "Class average mark:";
- Console::SetCursorPosition(10, 13); cout << "Class average grade:";
- Console::SetCursorPosition(10, 15); cout << "Average coursework mark:";
- Console::SetCursorPosition(10, 16); cout << "Average examination mark:";
- Console::SetCursorPosition(40, 6); cout << "[ ]";
- Console::SetCursorPosition(40, 7); cout << "[ ]";
- Console::SetCursorPosition(40, 8); cout << "[ ]";
- Console::SetCursorPosition(40, 9); cout << "[ ]";
- Console::SetCursorPosition(40, 10); cout << "[ ]";
- Console::SetCursorPosition(40, 12); cout << "[ ]";
- Console::SetCursorPosition(40, 13); cout << "[ ]";
- Console::SetCursorPosition(40, 15); cout << "[ ]";
- Console::SetCursorPosition(40, 16); cout << "[ ]";
- Console::SetCursorPosition(30, 24);
- cout << "Press any key to exit...";
- }
- void invalidCheck(int & mark, int row)
- {
- //If the user enters an invalid number, show an error message and
- //get another number from them
- while(mark < 0 || mark > 50)
- {
- //Show an error message
- Console::SetCursorPosition(60, row);
- cout << "Invalid number!";
- //Remove the number the user entered
- Console::SetCursorPosition(51, row);
- cout << " ]";
- //Get another number from the user
- Console::SetCursorPosition(51, row);
- cin >> mark;
- //Remove the error message
- Console::SetCursorPosition(60, row);
- cout << " ";
- }
- }
- void calcResult(int cwMark, int exMark, int & mark, string & grade, int & numStudents,
- int & cwSum, int & exSum, int & markSum, int gradeCount[])
- {
- //Calculate the total mark for the student
- mark = cwMark + exMark;
- //If the student scored lower than 40 but scored 15 in each component
- //allow them to pass
- if(mark < 40 && cwMark >= 15 && exMark >= 15)
- mark = 40;
- else
- //If the student scored 40 or more but did not score at least 15
- //in each component, fail them
- if(mark >= 40 && (cwMark < 15 || exMark < 15))
- mark = 39;
- //Calculate the grade
- if(mark >= 70)
- {
- grade = "A";
- //Increase the number of A's
- gradeCount[0]++;
- }
- else
- {
- if(mark >= 60)
- {
- grade = "B+";
- //Increase the number of B+'s
- gradeCount[1]++;
- }
- else
- {
- if(mark >= 50)
- {
- grade = "B";
- //Increase the number of B's
- gradeCount[2]++;
- }
- else
- {
- if(mark >= 40)
- {
- grade = "C";
- //Increase the number of C's
- gradeCount[3]++;
- }
- else
- {
- grade = "F";
- //Increase the number of F's
- gradeCount[4]++;
- }
- }
- }
- }
- //Add cwMark to cwSum
- cwSum += cwMark;
- //Add exMark to exSum
- exSum += exMark;
- //Add mark to markSum
- markSum += mark;
- //Increase the number of students
- numStudents++;
- }
- void calcAvg(double & avgMark, double & cwAvg, double & exAvg, string & avgGrade,
- int numStudents, int cwSum, int exSum, int markSum)
- {
- //Calculate class average mark
- avgMark = static_cast<double>(markSum) / numStudents;
- //Calculate the class average grade
- if(avgMark >= 70)
- avgGrade = "A";
- else
- if(avgMark >= 60)
- avgGrade = "B+";
- else
- if(avgMark >= 50)
- avgGrade = "B";
- else
- if(avgMark >= 40)
- avgGrade = "C";
- else
- avgGrade = "F";
- //Calculate the average coursework mark
- cwAvg = static_cast<double>(cwSum) / numStudents;
- //Calculate the average examination mark
- exAvg = static_cast<double>(exSum) / numStudents;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement