Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10COSC 181 – Foundations of Computer ProgrammingClass 2226.8 Case Study: Game of Chance and Introducing e n u mEnumerationA set of integer constants represented by identifiersThe values of enumeration constants start at 0, unless specified otherwise, and increment by 1The identifiers in an enum must be unique, but separate enumeration constants can have the same integer valueDefining an enumerationKeyword enum A type nameComma-separated list of identifier names enclosed in bracesExample•enum Months { JAN = 1, FEB, MAR, APR };31 // Fig. 6.11: f ig06_11.cpp2 / / Craps s imulat ion.3 #include <iostream>4 us ing std::cout;5 us ing std::endl;67 #include <cstdlib> // contains prototypes for functions srand and rand8 us ing std::rand;9 us ing std::srand;1011 #include <ctime> // contains prototype for function time12 us ing std::time; 1314 int rollDice(); // rolls dice, calculates amd displays sum1516 int main()17 {18 // enumeration with constants that represent the game status 19 enum Status { CONTINUE, WON, LOST }; // all caps in constants2021 int myPoint; // point if no win or loss on first roll22 Status gameStatus; // can contain CONTINUE, WON or LOST2324 // randomize random number generator using current time25 srand( time( 0 ) ); 2627 int sumOfDice = rollDice(); // first roll of the diceOutlinefig06_11.cpp (1 of 4)#include and using for function timeEnumeration to keep track of the game statusDeclaring a variable of the user-defined enumeration typeSeeding the random number generator with the current time42829 / / determ ine game status and point ( if needed) based on f ir s t ro l l30 switch ( sumOfDice ) 31 {32 case 7: // win with 7 on first roll33 case 11: // win with 11 on first roll 34 gameStatus = WON ;35 break;36 case 2: // lose with 2 on first roll37 case 3: // lose with 3 on first roll38 case 12: // lose with 12 on first roll 39 gameStatus = LOST;40 break;41 default: // did not win or lose, so remember point42 gameStatus = CONTINUE ; // game is not over43 myPoint = sumOfDice; // remember the point44 cout << "Po int is " << myPoint << endl;45 break; // optional at end of switch 46 } // end switch 4748 // while game is not complete49 while ( gameStatus == CONTINUE ) // not WON or LOST50 { 51 sumOfDice = rollDice(); // roll dice again52Outlinefig06_11.cpp (2 of 4)Assigning an enumeration constant to gameStatusComparing a variable of an enumeration type to an enumeration constant553 / / determine game status54 if ( sumOfDice == myPoint ) // win by making point55 gameStatus = WON ;56 else57 if ( sumOfDice == 7 ) // lose by rolling 7 before point58 gameStatus = LOST;59 } // end while 6061 // display won or lost message62 if ( gameStatus == WON )63 cout << "P layer wins" << endl;64 else65 cout << "P layer loses " << endl;6667 return 0; // indicates successful termination68 } // end main6970 // roll dice, calculate sum and display results71 int rollDice()72 {73 // pick random die values74 int die1 = 1 + rand() % 6; // first die roll75 int die2 = 1 + rand() % 6; // second die roll76 77 int sum = die1 + die2; // compute sum of die valuesOutlinefig06_11.cpp (3 of 4)Function that performs the task of rolling the dice67879 / / disp lay result s of th is ro l l80 cout << "Player rolled " << die1 << " + " << die281 << " = " << sum << endl;82 return sum; // end function rollDice83 } // end function rollD icePlayer rolled 2 + 5 = 7Player winsPlayer rolled 6 + 6 = 12Player losesPlayer rolled 3 + 3 = 6Point is 6Player rolled 5 + 3 = 8Player rolled 4 + 5 = 9Player rolled 2 + 1 = 3Player rolled 1 + 5 = 6Player winsPlayer rolled 1 + 3 = 4Point is 4Player rolled 4 + 6 = 10Player rolled 2 + 4 = 6Player rolled 6 + 4 = 10Player rolled 2 + 3 = 5Player rolled 2 + 4 = 6Player rolled 1 + 1 = 2Player rolled 4 + 4 = 8Player rolled 4 + 3 = 7Player losesOutlinefig06_11.cpp (4 of 4)7Good Programming Practice 6.1 Capitalize the first letter of an identifier used as a user-defined type name.8Good Programming Practice 6.2 Use only uppercase letters in the names of enumeration constants. This makes these constants stand out in a program and reminds the programmer that enumeration constants are not variables.9Good Programming Practice 6.3 Using enumerations rather than integer constants can make programs clearer and more maintainable. You can set the value of an enumeration constant once in the enumeration declaration.10Common Programming Error 6.9 Assigning the integer equivalent of an enumeration constant to a variable of the enumeration type is a compilation
View Full Document