1: /************************************************************************
     2:  * CS415 Assignment 9I
     3:  * Programmed by
     4:  *   Dan Noe
     5:  * 
     6:  * This non-interactive program accepts data about students and grades 
     7:  * on stdin and processes it to make a nicely formatted report including
     8:  * the students GPA (if they've taken courses).  If the student has not
     9:  * taken any courses a message to that effect is printed.
    10:  ************************************************************************/
    11: 
    12: #include <iostream> 
    13: 
    14: using namespace std;
    15: 
    16: void doStudent(); // functions used
    17: 
    18: int main() {
    19:    int numStudents;
    20:    
    21:    cin >> numStudents;
    22:    
    23:    for (int i=0; i < numStudents; i=i+1) {
    24:       doStudent();
    25:    }
    26:    
    27:    cout << "Correctly processed records for " << numStudents << " students\n";
    28:    
    29:    return 0;
    30: }
    31: 
    32: /************************************************************************
    33:  * This no argument proceedure performs the actual work of receiving
    34:  * student data from STDIN, performing weighted average calculations, and
    35:  * outputting the student's GPA.  If the student does not have any courses
    36:  * on record, a message is printed to STDOUT to that effect.
    37:  ************************************************************************/
    38: 
    39: void doStudent() {
    40:    int studentID, courseCredits;
    41:    int totalCredits = 0;
    42:    float courseGrade;
    43:    float totalPoints = 0;
    44: 
    45:    cin >> studentID;
    46:    cout << "id number " << studentID << ":\n";
    47:    cin >> courseGrade >> courseCredits;
    48:    
    49:    while (courseGrade >= 0 && courseCredits >= 0) {
    50:       cout << "  " << courseGrade << '/' <<  courseCredits;
    51:       
    52:       // Weight factor is number of credits per course.  We keep
    53:       // track of the total credits so we can divide the total
    54:       // by it later.
    55:       
    56:       totalCredits = totalCredits + courseCredits;
    57:       totalPoints = totalPoints + courseGrade * courseCredits;
    58:       
    59:       cin >> courseGrade >> courseCredits;
    60:    }
    61:    
    62:    // As a bonus, this next bit ensures we don't divide by zero :)
    63:    
    64:    if (totalCredits == 0) {
    65:       cout << "  Student number " << studentID;
    66:       cout << " does not have any course records.\n\n";
    67:    } else {
    68:       cout << "\n  GPA => " << totalPoints/totalCredits << "\n\n";
    69:    }
    70: }
    71: 




syntax highlighting by

w e b c p p
web c plus plus