Team 2550
Technical Documentation
|
|
Enumerations can make a program easier to read.
enum Color {RED, GREEN, BLUE, YELLOW, WHITE};
All an enumeration does is assign names to integer values. The only difference between an enum and a variable is that an enumeration creates its own datatype (in the above example, the enumeration creates a Color
datatype). When the program is compiled, the enum is converted to a plain integer.
enum
does is map words to an integer value, you can also assign specific values to enumerations. enum Distance { TOO_CLOSE = 10, TOO_FAR = 20 };
Because they are easy to read and do not slow down the code, enumerations are great for switch
statements.
In order to demonstrate the capabilities of cin
, I am going to run the above program.
Enter a distance: 10 You chose poorly: too close. Are you done? [y/n] n Enter a distance: 20 You chose poorly: too far. Are you done? [y/n] n Enter a distance: 15 You chose wisely. Goodbye.
This method, while not always apparent to the user, can be very useful for those who do know about it. You are actually able to provide a valid input set at the beginning of the program.
Enter a distance: 10 n 20 n 15 You chose poorly: too close. Are you done? [y/n] Enter a distance: You chose poorly: too far. Are you done? [y/n] Enter a distance: You chose wisely. Goodbye.