A commentary on my technical exploits

Tag Archives: case

In exercise 4 we step up our menu skills a notch or two. This time, we are using a struct to declare some members and we want to dynamically fill them using an array. Otherwise, this is very similar to exercise 3 in that we are still using switch case. There are some syntactical nuances you must navigate in order to get your output to display correctly and not enter a forever loop. Check out my source below:

4. When you join the Benevolent Order of Programmers, you can be known at BOP meetingsby your real name, your job title, or your secret BOP name. Write a program thatcan list members by real name, by job title, by secret name, or by a member’s preference.Base the program on the following structure:// Benevolent Order of Programmers name structurestruct bop {char fullname[strsize]; // real namechar title[strsize]; // job titlechar bopname[strsize]; // secret BOP nameint preference; // 0 = fullname, 1 = title, 2 = bopname};In the program, create a small array of such structures and initialize it to suitable values.Have the program run a loop that lets the user select from different alternatives:

a. display by name b. display by titlec. display by bopname d. display by preferenceq. quitNote that “display by preference” does not mean display the preference member; itmeans display the member corresponding to the preference number. For instance, ifpreference is 1, choice d would display the programmer’s job title. A sample run maylook something like the following:Benevolent Order of Programmers Reporta. display by name b. display by titlec. display by bopname d. display by preferenceq. quitEnter your choice: aWimp MachoRaki RhodesCelia LaiterHoppy HipmanPat HandNext choice: dWimp MachoJunior ProgrammerMIPSAnalyst TraineeLOOPYNext choice: qBye!

Exercise 3 is interesting because it shows us how a rudimentary menu can be made using simple switch statements. Check out the source for my solution:

3. Write a precursor to a menu-driven program. The program should display a menu offeringfour choices, each labeled with a letter. If the user responds with a letter other thanone of the four valid choices, the program should prompt the user to enter a validresponse until the user complies. Then the program should use a switch to select a simpleaction based on the user’s selection. A program run could look something like this:Please enter one of the following choices:c) carnivore p) pianistt) tree g) gamefPlease enter a c, p, t, or g: qPlease enter a c, p, t, or g: tA maple is a tree.