DEPARTMENT OF COMPUTING

CS 3005: Programming in C++

Menu System

Introduction

We want to have a text based menu system that allows the user to execute commands from a list of commands. Each command will manipulate the data stored in the program.

Assignment

In this assignment, you will add to the ApplicationData class, and create some new classes that support a menu system. You will also build a very simple program to test this menu system.

The commands this program will have are listed in this table.

Command Prefixable? Function Description
help no menuUI Display help message.
menu no menuUI Display help message.
# yes commentUI Skip to end of line (comment).
comment no commentUI Skip to end of line (comment).
echo no echoUI Echo back the arguments given.
quit no quitUI Terminate the program.

Example Session

$ ./program-menu-test/menu_test 
Choice? # this is a comment, it does nothing.
Choice? #this is a comment, where the command name is a prefix of the command typed.
Choice? comment this is a comment using the command name, not #
Choice? commentthis is a bad comment, because the "comment" command can not be a prefix.
Unknown action 'commentthis'. Use 'help' for a list of valid actions
Choice? echo Hello world!
 Hello world!
Choice? echo "Hello world!"
 "Hello world!"
Choice? echo copies everything after the echo command to the output.
 copies everything after the echo command to the output.
Choice? echo help will show the list of available commands
 help will show the list of available commands
Choice? help
Options are:
  # - Skip to end of line (comment).
  comment - Skip to end of line (comment).
  echo - Echo back the arguments given.
  help - Display help message.
  menu - Display help message.
  quit - Terminate the program.

Choice? echo menu does too
 menu does too
Choice? menu
Options are:
  # - Skip to end of line (comment).
  comment - Skip to end of line (comment).
  echo - Echo back the arguments given.
  help - Display help message.
  menu - Display help message.
  quit - Terminate the program.

Choice? quit

A Note on Comments

See the additional documentation below on script files.

When people write comments with the # command, sometimes they will write with a space after the hash, like this:

# COMMENT HERE

However, sometimes, people write comments without a leading space, like this:

#LAZY COMMENT HERE

When reading a command, our menu system will use white space (spaces, tabs, newlines) as word separators. In the first case, the # symbol is followed by a space, so it will be considered as a command name. We can then let the comment command handle the rest of the input line.

In the second case, the first space comes after #LAZY, so that is the command name. But #LAZY is not in our command list. If we don’t do something about this, the program will say that is an unknown command, then try to read the next word as a command, COMMENT is also not a command, so we get another error message. HERE is also not a command, so we get yet another error message.

So, let’s do something about it. We will tag the # command in our command list as a prefix command of length 1. When we scan to see if a user typed command is in the list of commands, if the first character (because length is 1), matches the command name #, we will consider it a # command. This way #LAZY is considered an alias for the #.

Commands that are not meant to function as prefix commands are will have a prefix length of 0 to indicate that they are not a prefix command.

A Note on Reference Returns

If a function returns a reference (&), the object being referenced must exist after the function ends.

Often, we use reference return types for a method of a class, when the method is returning a data member, or an element of a data member container. For example, we might return a reference to the value in a data member map associated with a key sent as a parameter.

A problem arises if the item the function has been asked to return does not exist. In this case, we still need to return a reference, or the function does not have valid syntax.

We could create a local variable on the stack to return, representing the error case. This is not correct. Remember all stack variables are removed as soon as the function returns. So, the reference would be to an object that no loner exists.

The solution is to create a variable that is locally scoped, but stored in the static section of memory. It will continue to exist after the function returns.

A Note on Clearing to EOL

See class discussion.

A Note on Input Streams

See class discussion.

Programming Requirements

Create library-application/ActionFunctionData.{h,cpp}

ActionFunctionData Class

This class represents one command, or action, that the user can execute. Think of it as a row in the table of commands listed above.

public Typedefs:

Data Members:

The following data members should be private or protected. public data members are not allowed.

public Methods:

Update library-application/Makefile

Add ActionFunctionData.{h,cpp} in the appropriate places to add them to the library and install the header file. Unless, of course, you have adopted the updated Makefile setup we built in class.

Create library-application/MenuData.{h,cpp}

MenuData Class

This class represents all of the commands the user can execute. Think of it as all of the rows in the table of commands listed above.

Data Members:

The following data members should be private or protected. public data members are not allowed.

protected Methods:

These methods must be protected, not public, and not private.

public Methods:

Update library-application/Makefile

Add MenuData.{h,cpp} in the appropriate places to add them to the library and install the header file. Unless, of course, you have adopted the updated Makefile setup we built in class.

Update library-application/ApplicationData.{h,cpp}

We will update the ApplicationData class to support a text menu based application.

ApplicationData Class

Data Members:

These additional data members should be in the private or protected section of the class. No public data members are allowed.

public Methods:

Create library-commands/menu_test_aux.{h,cpp}

Functions:

Update library-commands/Makefile

Add menu_test_aux.{h,cpp} in the appropriate places to add them to the library and install the header file. Unless, of course, you have adopted the updated Makefile setup we built in class.

Create program-menu-test/menu_test.cpp

Functions:

Create program-menu-test/Makefile

This file must contain rules such that any of the following commands will build the menu_test program:

Create program-menu-test/.gitignore

The file needs to store one line of text:

menu_test

This will prevent the executable program from being committed to the repository. It is a derived file.

Update Makefile

Additional Documentation

Grading Instructions

To receive credit for this assignment:

Extra Challenges (Not Required)

TBA

Last Updated 02/28/2025