/* --------------------------------------------------------------------- ** Figure 13.21: Selecting ice cream from a menu. ** Figure 13.22: A menu-handling function. ** --------------------------------------------------------------------- */ #include "tools.h" /* contains definition of type string */ #define CHOICES 6 int menu ( string title, int max, string menu_list[] ); void main( void ) { int choice; /* The menu selection */ string greeting = "\n I'm happy to serve you. Our specials today are: "; string flavor[CHOICES] = { "Empty", "Vanilla", "Pistachio", "Rocky Road", "Fudge Chip", "Lemon" }; float price[CHOICES] = { 0.00, 1.00, 1.50, 1.35, 1.25, 1.20 }; choice = menu( greeting, CHOICES, flavor ); printf( "\n Here is your %s cone. That will be $%.2f. \n", flavor[choice], price[choice] ); puts( " Thank you, come again. \n" ); } /* ---------------------------------------------------------------------- */ /* Display a menu then read and validate a numeric menu choice. */ int menu ( string title, int max, string menu_list[] ) { int choice; /* To store the menu selection. */ int n = 0; /* Loop counter for menu display. */ printf( "\n %s\n", title ); for (n=0; n < max; ++n) printf( "\t %i. %s \n", n, menu_list[n] ); printf( " Please enter your selection: "); for(;;) { /* Prompt for and validate a menu selection. */ scanf( "%i", &choice ); if (choice >= 0 && choice < max) break; /* Accept valid choice. */ printf( " Please enter number between 0 and %i: ", max-1 ); } return choice; }