// Mr. Minich // Computer Science Using C++ // Ch. 10 Demo Program #5 // December 5, 2000 // Purpose - to illustrate the use of an enumerated data type (an enumeration) and passing // struct variables by reference #include<iostream.h> #include <stdlib.h> #include <time.h> enum Division {AFC_EAST, AFC_CENTRAL, AFC_WEST, NFC_EAST, NFC_CENTRAL, NFC_WEST}; struct Team { int myWins; int myLosses; Division myDivision; }; void playGame(Team &, Team &); void initializeTeam(Team &, Division); int main() { Team dolphins; // Miami Dolphins NFL football team Team eagles; // Philadelphia Eagles NFL football team initializeTeam(dolphins, AFC_EAST); initializeTeam(eagles, NFC_EAST); playGame(dolphins, eagles); cout << "The Dolphins' record is " << dolphins.myWins << " - " << dolphins.myLosses << " which is the best in the " << dolphins.myDivision << " division." << endl; // It is an error for the programmer to believe that "AFC_EAST" will display in the // cout statement above. The actual values of enumerated types will display as // their integer values not as the value specified. // What is the value of dolphins.myDivision that displays above? return 0; }// end of main void initializeTeam(Team &anyTeam, Division anyDivision) { anyTeam.myWins = 0; anyTeam.myLosses = 0; anyTeam.myDivision = anyDivision; }// end of initializeTeam void playGame(Team &home, Team &visitors) { srand(time(0)); if (rand() % 2 == 1) // 50% chance of home team winning { home.myWins++; visitors.myLosses++; } else { home.myLosses++; visitors.myWins++; } }// end of playGame // The struct variables are passed by reference (using the & operator) // rather than being passed by value because it is desired to have // the functions permanently change the values of the struct members. // Often, it is usually recommended to pass struct variables by reference // rather than by value because it saves memory.