Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is the Windows version of the student averages program. To compile on UNIX/Linux-based OSes, change any instance of "CLS" to "clear" and remove the system("PAUSE") line.
- //This program will allow you to enter information on eleven students - names, and grades on five quizzes - and then it will calculate some averages.
- //Call the input function - we're using a global structure for this, for ease of coding.
- //After fetching input, we'll ask if they want to input another student. This is limited, as of present, to ten students - we have a counter not only as debug, but to allow the user to track how much has been input so they know what happens when the loop suddenly ceases.
- //If the user says y/Y (thank you, tolower!) we will repeat the loop -- if the user says n/N, we will display the output.
- //DispLaying output: this is tabulated for ease of reading.
- //Calculate the overall averages in their own functions, and display them.
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <iomanip>
- #include <stdlib.h> // This is for cross-compatibility between OSes. system calls seemed to break on Linux without it.
- using namespace std;
- struct studentinfo
- {
- string fname;
- string lname;
- int id;
- double tests[5];
- double average;
- };
- studentinfo students[10];
- double overallavg[6];
- void getinput (int counter);
- char again (char resp);
- void displayI (int counter, int stotal);
- void displayII (int counter, int stotal);
- int main() {
- int counter; // god I worry about my coding ability sometimes
- int stotal; // total students for display loop
- char resp; // another Y/N loop
- counter = 0; // Initialize it, as a precaution.
- do {
- getinput(counter);
- resp = again(resp);
- counter++;
- stotal++;
- } while (resp == 'y' && counter < 10);
- system("CLS");
- displayI(counter, stotal);
- displayII(counter, stotal);
- system("PAUSE");
- }
- void getinput (int counter) {
- //This function merely reads the user's input - First & last names, ID number (for sake of expansion later on), and the five test scores.
- int i;
- int testno;
- testno = 0;
- cout << counter << " of 10 added\n\n";
- cout << "Enter the first name: ";
- cin >> students[counter].fname;
- cout << "Enter the last name: ";
- cin >> students[counter].lname;
- cout << "Enter the ID: ";
- cin >> students[counter].id;
- for (i=0;i<5;i++) {
- testno++;
- cout << "Enter the score for test " << testno << " of 5: ";
- cin >> students[counter].tests[i];
- }
- //And then we need to actually calculate the average... averages are stored in the student struct, again, for ease of coding (and organization).
- for (i=0;i<5;i++) students[counter].average+=students[counter].tests[i]/5;
- }
- void displayI (int stotal, int counter) {
- //Display the bulk of the table. This includes headings, names, IDs, test scores, and per-student averages. Tabulated with \t for ease of reading, as well.
- int i;
- counter = 0;
- cout << setw(5) << left << "ID" << setw(40) << "Student Name" << setw(5) << right << "1" << setw(5) << "2" << setw(5) << "3" << setw(5) << "4" << setw(5) << "5" << setw(9) << "Average";
- cout << "\n"; // just to make things align better
- // We're not using a for loop below - the screen is typically 80 characters on Windows / MS-DOS systems, and it would be tough to fit more tests.
- do {
- cout << setw(5) << left << students[counter].id << setw(40) << (students[counter].lname + ", " + students[counter].fname).substr(0,40) << setw(5) << right << students[counter].tests[0] << setw(5) << students[counter].tests[1] << setw(5) << students[counter].tests[2] << setw(5) << students[counter].tests[3] << setw(5) << students[counter].tests[4] << setw(9) << setprecision(3) << students[counter].average << "\n";
- counter++;
- } while (counter < stotal); // stotal: this increases with each student input, and its purpose is to tell this function when to stop. Also used in calculating overall averages.
- }
- void displayII (int stotal, int counter) {
- // Calculate all of the overall averages, then display them. I would have done this in the main display function, and another way, but requirements called for it to have its own function.
- int i;
- counter = 0;
- do {
- for (i=0; i<5; i++) overallavg[i]+=students[counter].tests[i]/stotal;
- counter++;
- } while (counter < stotal);
- //overallavg[5] =
- for (i=0; i<5; i++) overallavg[5]+=overallavg[i]/5;
- cout << setw(45) << left << "Overall averages:" << setw(5) << right << overallavg[0] << setw(5) << overallavg[1] << setw(5) << overallavg[2] << setw(5) << overallavg[3] << setw(5) << overallavg[4] << setw(9) << overallavg[5] << "\n\n";
- }
- char again(char resp) {
- // This function will ask if the user wishes to add another grade. (Ripped from the replace program, but what programmer doesn't reuse code when needed?)
- // char resp;
- do {
- cout << "\nAdd another? (Y/N, will repeat on other answers): ";
- cin >> resp;
- cin.ignore(50, '\n');
- resp = tolower(resp); // Shift it to lowercase so there are fewer cases to test for.
- } while (resp != 'y' && resp != 'n');
- system("PAUSE");
- system("CLS"); // Linux/UNIX users: replace CLS with clear
- return resp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement