CSC 161 Program #3

"TextMenu"

TextMenu is a nice and easy 3rd program, and maybe it's some code you will reuse in the future.

 


Logistics

This program is worth 4 points, or 4% of your final grade. The pertinent time coordinates:

  • Assigned: Apr 28, 2005
  • Program due: May 5, 2005 (1 week!)

Some of the fun, new stuff we'll cover includes:

  • Abstract base class
  • Virtual functions
  • Pure virtual functions
  • Vectors

  



Description

In Program 3, you will create classes that can be used to create text menus. A text menu is like the interface you probably had in the MorseCode program:

1. Char to Morse
2. Line to Morse
   ...and so on...
7. Quit
Enter menu choice>
 

Usually this kind of menu stuff happens in main(). Something like this:

   setup menu
   while( not quit) {
      display menu
      get user's menu choice
      switch on that choice {
      case <num>: do something
      }
   }

Please assume there is another coder in the wings (let's call her Jane Windows) who wishes to use your code for a Windows-based graphics menu system. This means that you will want to define a abstract base class called Menu and a subclass TextMenu. Jane would then theoretically create her own subclass WindowsMenu, or something like that.

Get it? Some more requirements:

  • Please use the vector class to store the choices in your Menu.
  • Displaying the menu would be a TextMenu thing, right? Display would be very different for Jane Windows.
  • Please create some static functions (probably in TextMenu) to prompt the user for a string, an integer, a float, and a Yes/No response. You can even create query functions that prompt the user for an input or output file. (Hey, why are these functions could choices for static designation?) Some of my query member functions of TextMenu sort of looked like this:
    • void query( int &, char *prompt = 0);
    • void query( float &, char *prompt = 0);
    • void query( ifstream &, char *prompt = 0);
  • Create a test driver that exercises each member function in TextMenu.

  


Design Requirements

The vector class is a part of something called the "Standard Template Library". It's a collection of useful classes that are supported by most C++ compilers. Vectors are like a cool array that gets as big as you need it. It analogous to the convenience C++ strings provide over C-strings. You can read all about vectors starting on page 528 of our text. Check it out!

I used a vector object to store the strings that were my menu choices. Use the vector to store pointers to string objects, rather than the string objects themselves, like this:

vector<string *> choices;

Using string pointers saves your program from copying every string you create.

  


Grading

Short program this time:
  • Quality, 50%
  • Function, 50%
Each of these areas is discussed in the handout: CSC 161 Program Design, Style and Testing. Please leave a README file in your folder describing your testing and the state of your program... problems, questions, etc.

thanks... yow, bill

 


Notes

Apr 28, 2005

One of the nice side effect of reusable code (like our little TextMenu here) is the pressure put on the person developing the software to make it bullet-proof.

Here's a little code snippet in an effort to make our TextMenu code more hearty... getting the user's menu choice:

int choice;
cout << "Enter menu choice> ";
cin >> choice;
if( !cin.good()) { cin.clear(); }
cin.ignore( 100, '\n');

Let's examine this code line by line:

  1. An integer
  2. Print "Enter Menu Choice"
  3. Use the input stream operator to get an integer choice
  4. If cin was damaged (not good()), then call clear() to reset all the cin flags so it will work again.
  5. Of course, we want to consume the rest of the line (well, up to 100 chars), including the newline, so that our next input request works as advertised.

See, cin carries its status, or how it is doing, along with it. When cin has a problem one of its flags is set and it stops working until the flag is cleared.

try it... yow, bill

Coming soon... fixing Bill and Eric's fstream reuse concerns from Morse Code.

May 5, 2005

It is acceptable to store a vector of strings rather than string pointers. I'll talk more about this in class. Like this:

vector<string> choices;

thanks... yow, bill