C++: Menu


Given a list containing a number of strings of which one is to be selected and a prompt string, create a function that:

  • Print a textual menu formatted as an index value followed by its corresponding string for each item in the list.
  • Prompt the user to enter a number.
  • Return the string corresponding to the index number.

The function should reject input that is not an integer or is an out of range integer index by recreating the whole menu before asking again for a number. The function should return an empty string if called with an empty list.

For test purposes use the four phrases: “fee fie”, “huff and puff”, “mirror mirror” and “tick tock” in a list.

Note: This task is fashioned after the action of the Bash select statement.

#include <iostream>
#include <boost/regex.hpp>
#include <cstdlib>
#include <string>
using namespace std;

void printMenu(const string *, int);
//checks whether entered data is in required range
bool checkEntry(string, const string *, int);

string dataEntry(string prompt, const string *terms, int size) {
	if (size == 0) { //we return an empty string when we call the function with an empty list
		return "";

	string entry;
	do {
		printMenu(terms, size);
		cout << prompt;

		cin >> entry;
	while( !checkEntry(entry, terms, size) );

	int number = atoi(entry.c_str());
	return terms[number - 1];

void printMenu(const string *terms, int num) {
	for (int i = 1 ; i < num + 1 ; i++) {
		cout << i << ')' << terms[ i - 1 ] << '\n';

bool checkEntry(string myEntry, const string *terms, int num) {
	boost::regex e("^\\d+$");
	if (!boost::regex_match(myEntry, e)) 
	return false;
	int number = atoi(myEntry.c_str());
	if (number < 1 || number > num) 
	return false;
	return true;

int main( ) {
	const string terms[ ] = { "fee fie" , "huff and puff" , "mirror mirror" , "tick tock" };
	int size = sizeof terms / sizeof *terms;
	cout << "You chose: " << dataEntry("Which is from the three pigs: ", terms, size);
	return 0;


Content is available under GNU Free Documentation License 1.2.