Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.26 KB | None | 0 0
  1. //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.
  2. //This program will allow you to enter information on eleven students - names, and grades on five quizzes - and then it will calculate some averages.
  3.  
  4. //Call the input function - we're using a global structure for this, for ease of coding.
  5. //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.
  6. //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.
  7. //DispLaying output: this is tabulated for ease of reading.
  8. //Calculate the overall averages in their own functions, and display them.
  9.  
  10. #include <iostream>
  11. #include <string>
  12. #include <cstring>
  13. #include <iomanip>
  14. #include <stdlib.h> // This is for cross-compatibility between OSes. system calls seemed to break on Linux without it.
  15.  
  16. using namespace std;
  17.  
  18. struct studentinfo
  19. {
  20.     string fname;
  21.     string lname;
  22.     int id;
  23.     double tests[5];
  24.     double average;
  25. };
  26.  
  27.  
  28. studentinfo students[10];
  29. double overallavg[6];
  30.  
  31. void getinput (int counter);
  32. char again (char resp);
  33. void displayI (int counter, int stotal);
  34. void displayII (int counter, int stotal);
  35.  
  36. int main() {
  37.      
  38. int counter; // god I worry about my coding ability sometimes
  39. int stotal; // total students for display loop
  40. char resp; // another Y/N loop
  41.  
  42. counter = 0; // Initialize it, as a precaution.
  43.  
  44. do {
  45.     getinput(counter);
  46.     resp = again(resp);
  47.     counter++;
  48.     stotal++;
  49. } while (resp == 'y' && counter < 10);
  50.  
  51. system("CLS");
  52. displayI(counter, stotal);
  53. displayII(counter, stotal);
  54. system("PAUSE");
  55. }
  56.  
  57. void getinput (int counter) {
  58. //This function merely reads the user's input - First & last names, ID number (for sake of expansion later on), and the five test scores.
  59.        int i;
  60.        int testno;
  61.        testno = 0;
  62.        cout << counter << " of 10 added\n\n";
  63.        cout << "Enter the first name: ";
  64.        cin >> students[counter].fname;
  65.        cout << "Enter the last name: ";
  66.        cin >> students[counter].lname;
  67.        cout << "Enter the ID: ";
  68.        cin >> students[counter].id;
  69.        for (i=0;i<5;i++) {
  70.        testno++;
  71.        cout << "Enter the score for test " << testno << " of 5: ";
  72.        cin >> students[counter].tests[i];
  73.        }
  74.        //And then we need to actually calculate the average... averages are stored in the student struct, again, for ease of coding (and organization).
  75.        for (i=0;i<5;i++) students[counter].average+=students[counter].tests[i]/5;
  76. }
  77.  
  78. void displayI (int stotal, int counter) {
  79. //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.
  80.      int i;
  81.      counter = 0;
  82.      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";
  83.      cout << "\n"; // just to make things align better
  84. // 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.
  85.      do {
  86.      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";
  87.      counter++;
  88.      } 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.
  89.  
  90. }
  91. void displayII (int stotal, int counter) {
  92. // 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.
  93.     int i;
  94.     counter = 0;
  95.     do {
  96.     for (i=0; i<5; i++) overallavg[i]+=students[counter].tests[i]/stotal;
  97.     counter++;
  98.     } while (counter < stotal);
  99.     //overallavg[5] =
  100.      for (i=0; i<5; i++) overallavg[5]+=overallavg[i]/5;
  101.     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";
  102. }
  103.  
  104. char again(char resp) {
  105. // 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?)
  106. //     char resp;
  107. do {
  108.  
  109. cout << "\nAdd another? (Y/N, will repeat on other answers): ";
  110. cin >> resp;
  111. cin.ignore(50, '\n');
  112. resp = tolower(resp); // Shift it to lowercase so there are fewer cases to test for.
  113. } while (resp != 'y' && resp != 'n');
  114.  system("PAUSE");
  115.  system("CLS"); // Linux/UNIX users: replace CLS with clear
  116. return resp;
  117.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement