Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22COSC 181 – Foundations of Computer ProgrammingClass 1721 // Fig. 5.9: GradeBook.h2 / / Def in it ion of class GradeBook that counts A, B, C, D and F grades.3 / / Member funct ions are def ined in GradeBook.cpp45 #inc lude <string> // program uses C++ standard string class6 us ing std::string;78 // GradeBook class definition9 clas s GradeBook10 {11 public:12 GradeBook( string ); // constructor initializes course name13 void setCourseName( string ); // function to set the course name14 string getCourseName(); // function to retrieve the course name15 void displayMessage(); // display a welcome message16 void inputGrades(); // input arbitrary number of grades from user 17 void displayGradeReport(); // display a report based on the grades18 pr ivate:19 string courseName; // course name for this GradeBook20 int aCount; // count of A grades21 int bCount; // count of B grades22 int cCount; // count of C grades23 int dCount; // count of D grades24 int fCount; // count of F grades25 }; // end class GradeBook Outlinefig05_09.cpp(1 of 1)Counter variable for each grade category31 // Fig. 5.10: GradeBook.cpp2 / / Member- funct ion def in i t i ons for class GradeBook that3 / / uses a sw itch statement to count A, B, C, D and F grades.4 #inc lude <iostream>5 us ing std::cout;6 us ing std::cin;7 us ing std::endl;89 #inc lude "GradeBook.h" // include definition of class GradeBook1011 // constructor initializes courseName with string supplied as argument;12 // initializes counter data members to 013 GradeBook::GradeBook( string name )14 {15 setCourseName( name ); // validate and store courseName16 aCount = 0; // initialize count of A grades to 017 bCount = 0; // initialize count of B grades to 018 cCount = 0; // initialize count of C grades to 019 dCount = 0; // initialize count of D grades to 020 fCount = 0; // initialize count of F grades to 021 } // end GradeBook constructor22Outlinefig05_10.cpp(1 of 5)Initialize each counter variable to 0423 / / funct ion to set the course name; lim it s name to 25 or fewer characters24 void GradeBook::setCourseName( string name )25 {26 if ( name.length() <= 25 ) // if name has 25 or fewer characters27 courseName = name; // store the course name in the object28 else // if name is longer than 25 characters29 { // set courseName to first 25 characters of parameter name30 courseName = name.substr( 0, 25 ); // select first 25 characters31 cout << "Name \"" << name << "\" exceeds maximum length (25).\n"32 << "Limiting courseName to first 25 characters.\n" << endl;33 } // end if...else34 } // end function setCourseName3536 // function to retrieve the course name37 string GradeBook::getCourseName()38 {39 return courseName;40 } // end function getCourseName4142 // display a welcome message to the GradeBook user43 void GradeBook::displayMessage()44 {45 // this statement calls getCourseName to get the 46 // name of the course this GradeBook represents47 cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" 48 << endl;49 } // end function displayMessage50Outlinefig05_10.cpp(2 of 5)551 / / input arbit ra r y number of grades from user; update grade counter52 vo id GradeBook::inputGrades()53 {54 int grade; // grade entered by user5556 cout << "Enter the let t er grades. " << endl57 << "Enter the EOF character to end input . " << endl;5859 // loop until user types end-of-file key sequence60 while ( ( grade = cin.get() ) != EOF )61 {62 // determine which grade was entered 63 sw it ch ( grade ) // switch statement nested in while64 { 65 case 'A': // grade was uppercase A 66 case 'a': // or lowercase a 67 aCount++; // increment aCount 68 break; // necessary to exit switch 69 70 case 'B': // grade was uppercase B 71 case 'b': // or lowercase b 72 bCount++; // increment bCount 73 break; // exit switch 74 75 case 'C': // grade was uppercase C 76 case 'c': // or lowercase c 77 cCount++; // increment cCount 78 break; // exit switch 79 Outlinefig05_10.cpp(3 of 5)Loop condition uses function cin.get to determine whether there is more data to inputswitch statement determines which case label to execute, depending on controlling expressiongrade is the controlling expressioncase labels for a grade of Abreak statement transfers control to after the end of the switch statement680 case 'D': // grade was uppercase D 81 case 'd': // or lowercase d 82 dCount++; // increment dCount 83 break; // exit switch 84 85 case 'F': // grade was uppercase F 86 case 'f': // or lowercase f 87 fCount++; // increment fCount 88 break; // exit switch 89 90 case '\n': // ignore newlines, 91 case '\t': // tabs, 92 case ' ': // and spaces in input 93 break; // exit switch 94 95 default: // catch all other characters 96 cout << "Incorrect letter grade entered." 97 << " Enter a new grade." << endl; 98 break; // optional; will exit switch anyway 99 } // end switch 100 } // end while101} // end function inputGradesOutlinefig05_10.cpp(4 of 5)default case for an invalid letter gradeIgnore whitespace characters, do not display an error message7102103/ / disp lay a report based on the grades entered by user104void GradeBook::displayGradeReport()105{106 / / output summary of result s107 cout << "\n\nNumber of students who rece ived each let t e r grade: " 108 << "\nA: " << aCount // display number of A grades109 << "\nB: " << bCount // display number of B grades110 << "\nC: " << cCount // display number of C grades111 << "\nD: " << dCount // display number of D grades112 << "\nF : " << fCount // display number


View Full Document

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?