DOC PREVIEW
UVa-Wise COSC 181 - Foundations of Computer Programming

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

COSC 181 Foundations of Computer Programming Class 17 1 Fig 5 9 GradeBook h 2 Def in it io n of c lass GradeBook that counts A B C D and F grades 3 Member funct ions are def ined in GradeBook cpp 4 5 inc lude string program uses C standard string class 6 us ing std string fig05 0 7 8 GradeBook class definition 9 c lass GradeBook 9 cpp 10 11 public 12 GradeBook string constructor initializes course name 13 vo id setCourseName string function to set the course name 14 string getCourseName function to retrieve the course name 15 vo id displayMessage display a welcome message 16 vo id inputGrades input arbitrary number of grades from user 17 vo id displayGradeReport display a report based on the grades Outline 1 of 1 18 pr iva te 19 string courseName course name for this GradeBook 20 int aCount count of A grades 21 int bCount count of B grades 22 int cCount count of C grades 23 int dCount count of D grades 24 int fCount count of F grades Counter variable for each grade category 25 end class GradeBook 2 1 Fig 5 10 GradeBook cpp 2 Member funct ion def in i t i o ns for c lass GradeBook that 3 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 8 9 inc lude GradeBook h include definition of class GradeBook 10 Outline fig05 1 0 cpp 11 constructor initializes courseName with string supplied as argument 12 initializes counter data members to 0 13 GradeBook GradeBook string name 14 15 setCourseName name validate and store courseName 16 aCount 0 initialize count of A grades to 0 17 bCount 0 initialize count of B grades to 0 18 cCount 0 initialize count of C grades to 0 19 dCount 0 initialize count of D grades to 0 20 fCount 0 initialize count of F grades to 0 1 of 5 Initialize each counter variable to 0 21 end GradeBook constructor 22 3 23 funct ion to set the course name l im it s name to 25 or fewer characters 24 vo id GradeBook setCourseName string name 25 26 if name length 25 if name has 25 or fewer characters 27 courseName name store the course name in the object 28 else if name is longer than 25 characters 29 30 31 32 set courseName to first 25 characters of parameter name courseName name substr 0 25 select first 25 characters cout Name name exceeds maximum length 25 n Limiting courseName to first 25 characters n endl 33 end if else 34 end function setCourseName 35 36 function to retrieve the course name 37 string GradeBook getCourseName Outline fig05 1 0 cpp 2 of 5 38 39 return courseName 40 end function getCourseName 41 42 display a welcome message to the GradeBook user 43 void GradeBook displayMessage 44 45 this statement calls getCourseName to get the 46 name of the course this GradeBook represents 47 cout Welcome to the grade book for n getCourseName n 48 endl 49 end function displayMessage 50 4 51 input arb it r a r y number of grades from user update grade counter 52 vo id GradeBook inputGrades 53 54 int grade grade entered by user 55 56 cout Enter the le t t e r grades endl 57 Enter the EOF character to end input endl 58 59 loop until user types end of file key sequence Loop condition uses function cin get to 60 while grade cin get EOF determine whether there is more data to input 61 62 determine which grade was entered switch statement determines 63 sw itch grade switch statement nested in while which case label to execute 64 65 case A grade was uppercase A depending on controlling expression 66 case a or lowercase a 67 aCount increment aCount grade is the controlling expression 68 break necessary to exit switch 69 Outline fig05 1 0 cpp 3 of 5 70 case B grade was uppercase B 71 72 73 74 75 76 77 78 79 case b or lowercase b bCount increment bCount break exit switch case labels for a grade of A break statement transfers control to after the end of the switch statement case C grade was uppercase C case c or lowercase c cCount increment cCount break exit switch 5 80 case D grade was uppercase D 81 case d or lowercase d 82 dCount increment dCount 83 break exit switch Outline 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 fig05 1 0 cpp Ignore whitespace characters do not display an error message default case for an invalid letter grade 4 of 5 94 95 96 97 98 99 100 default catch all other characters cout Incorrect letter grade entered Enter a new grade endl break optional will exit switch anyway end switch end while 101 end function inputGrades 6 102 103 disp lay a report based on the grades entered by user 104vo id GradeBook displayGradeReport 105 106 107 cout n nNumber of students who rece ived each le t t e r grade Outline output summary of resu lt s 108 nA aCount display number of A grades 109 nB bCount display number of B grades 110 nC cCount display number of C grades 111 nD dCount display number of D grades 112 nF fCount display number of F grades 113 endl 114 end funct ion displayGradeReport fig05 0 1 cpp 5 of 5 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Fig 5 11 f ig05 11 cpp Create GradeBook object input grades and disp lay grade report inc lude GradeBook h include definition of class GradeBook int main create GradeBook object GradeBook myGradeBook CS101 C Programming myGradeBook displayMessage display welcome message myGradeBook inputGrades read grades from user myGradeBook displayGradeReport display report based on grades return0 indicate successful termination end main Outline fig05 1 1 cpp 1 of 2 8 Welcome to the grade book for CS101 C Programming Enter the letter grades Enter the EOF character to end input a B An error message is shown in c response to an invalid grade C A d f C E Incorrect letter grade entered Enter a new grade D A b Z Outline fig05 1 1 cpp 2 of 2 Number of students who received each letter grade A 3 B 2 C 3 D 2 9 5 6 switch Multiple Selection Statement Cont Reading character input Function cin get Reads one character from the keyboard Integer value of a character static cast int ASCII character set character Table of characters and their decimal equivalents EOF ctrl d in UNIX Linux ctrl z in Windows 10 5 6 switch Multiple Selection Statement Cont switch statement Controlling expression Expression in parentheses after …


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 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?