Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I fix my menus and submenus?

Posted on 2008-10-05
8
Medium Priority
?
508 Views
Last Modified: 2010-04-21
I would like someone to take a look at my code to review it. The situation is pretty obvious:
When the program starts, it displays the main menu, then a submenu appears depending of the selected choice. The problem occurs when the user tries to exit and return to the main program. Instead, the program acts on a weird way and I have to stop debugging.

///////////////////////////////
//StackInterface.h; header file
 
#ifndef H_StackInterface
#define H_StackInterface
 
#include <iostream>
#include "LinkedStack.h"
 
using namespace std;
 
template<class Type>
class StackInterface
{
public:
	void displayMenu();
		//Function to display the main menu
	void runTest();
		//Function to run test for different stack function
private:
	int input;
		//Variable to store the number provided by user
	int option;
		//Variable to store user's selected option
};
 
template<class Type>
void StackInterface<Type>::displayMenu()
{
	cout << "Menu: " << endl << endl;
	cout << "1) Insert an item " << endl;
	cout << "2) Remove an item " << endl;
	cout << "3) Display items on stack " << endl << endl;
	cout << "0) Exit program " << endl;
}//end method displayMenu
 
template<class Type>
void StackInterface<Type>::runTest()
{
	linkedStackType<int> intStack;
	linkedStackType<int> tempStack;
 
	StackInterface controlStack;
 
	cout << endl << "Select your option: " << endl;
	cin >> option;
	
	while (option != 0)
	{
		switch (option)
		{
		case 1:	//Inserts an item on top of the stack
			cout << endl << "What number do you want to store? " << endl;
			cin >> input;
			intStack.push(input);
			break;
		case 2: //Removes the item on top of the stack
			intStack.pop();
			break;
		case 3: //Display the items currently stored on stack
			tempStack = intStack;
 
			cout << endl << "The items on stack are: ";
			
			while (!tempStack.isEmptyStack())
			{
				cout << tempStack.top() << " ";
				tempStack.pop();
			}
			cout << endl;
			break;
		default:
			cout << endl << "Invalid selection!" << endl;
		}//end switch
		
		
		system("PAUSE");	//Press any key to continue...
		system("CLS");		//Clear screen
		controlStack.displayMenu();
		cout << endl << "Select your option: " ;
		cin >> option;
		
	}//end while
}//end method runTest
 
#endif
///////////////////////////////////////////
#ifndef H_queueInterface
#define H_queueInterface
 
#include <iostream>
#include "LinkedQueue.h"
 
using namespace std;
 
template<class Type>
class QueueInterface
{
public:
	void displayMenu();
		//Function to display the main menu
	void runTest();
		//Function to run test for different queue function
private:
	int newElement;
		//Variable to store the number provided by user
	int queueSize;
		//Variable to determine the amount of elements to store
	int choice;
		//Variable to store user's selected option
};
 
template<class Type>
void QueueInterface<Type>::displayMenu()
{
	cout << "Menu: " << endl << endl;
	cout << "1) Insert element(s) " << endl;
	cout << "2) Remove an element " << endl;
	cout << "3) Display stored elements " << endl << endl;
	cout << "0) Exit program " << endl;
}//end method displayMenu
 
template<class Type>
void QueueInterface<Type>::runTest()
{
	linkedQueueType<int> intQueue;
	QueueInterface controlQueue;
 
	cout << endl << "Select your option: " << endl;
	cin >> choice;
	
	while (choice != 0)
	{
		switch (choice)
		{
		case 1:	//Inserts element in the front of the queue
			cout << "Enter the size of your queue: " << endl;
			cin >> queueSize;
			
			for (int counter = 0; counter < queueSize; counter++)
			{
				cout << endl << "Enter a new element: " << endl;
				cin >> newElement;
				intQueue.addQueue(newElement);
			}//end for
			break;
		case 2: //Removes an element from the queue
			if (!intQueue.isEmptyQueue())	//If the queue is not empty...
			{
				intQueue.deleteQueue();		//delete the next element in the queue
				cout << "Item deleted!" << endl;
			}
			else
				cout << "The queue is empty! " << endl;
			break;
		case 3:	//Display the items currently stored on queue	
			cout << endl << "Elements on queue: ";
 
			while (!intQueue.isEmptyQueue())				//If the queue is not empty...
			{
				cout << intQueue.front() << " ";	//...print elements in the form "x x x ... x"
				intQueue.deleteQueue();
			}//end while
			cout << endl;
			break;
		default:
			cout << endl << "Invalid selection!" << endl;
		}//end switch
 
		system("PAUSE");	//Press any key to continue...
		system("CLS");		//Clear screen
		
		controlQueue.displayMenu();
		cout << endl << "Select your option: " ;
		cin >> choice;
 
	}//end while
}//end method runTest
 
#endif
////////////////////////////////////////////////////
#ifndef H_TreeInterface
#define H_TreeInterface
 
#include <iostream>
#include "BinarySearchTree.h"
 
using namespace std;
 
template<class Type>
class TreeInterface
{
public:
	void displayMenu();
	void runTest();
protected:
	int num;
	int nodes;
	int selection;
};
 
template<class Type>
void TreeInterface<Type>::displayMenu()
{
	cout << "Binary Search Tree Creator " << endl << endl;
	cout << "1) Create a binary search tree of n nodes " << endl;
	cout << "2) Show original tree " << endl;
	cout << "3) Swap subtrees " << endl << endl;
	cout << "0) Exit program " << endl;
}
 
template<class Type>
void TreeInterface<Type>::runTest()
{
	bSearchTreeType<int> intTree;
	TreeInterface controlTree;
 
	cout << endl << "Select your option: " << endl;
	cin >> selection;
	
	while (selection != 0)
	{
		switch (selection)
		{
		case 1:	
			cout << "How many nodes?" << endl;
			cin >> nodes;
 
			for (int counter = 0; counter < nodes; counter++)
			{
				cout << endl << "Enter a number: ";
				cin >> num;
				intTree.insert(num);
			}//end for
	
			break;
		case 2: 
			//Show the tree as it was captured
			cout << endl << "Tree: " ;
			intTree.printTree();
			break;
		case 3: 
			//Call function to swap the subtrees
			cout << endl << "Swap subtrees..." << endl;
			intTree.swapSubtrees();
 
			//Print the binary search tree with the subtrees already swapped
			cout << endl << "Swapped tree: " ;
			intTree.printTree();
			break;
		default:
			cout << endl << "Invalid selection!" << endl;
		}//end switch
		system("PAUSE");	//Press any key to continue...
		system("CLS");		//Clear screen
		
		controlTree.displayMenu();
		cout << endl << "Select your option: ";
		cin >> selection;
	}//end while
}//end method runTest
 
#endif
/////////////////////////////////////////////////////////
#ifndef H_MainInterface
#define H_MainInterface
 
#include <iostream>
#include "StackInterface.h"
#include "QueueInterface.h"
#include "TreeInterface.h"
 
using namespace std;
 
class MainInterface
{
public:
	void showMainMenu();
	void runSubtests();
protected:
	char mainOption;
};
 
void MainInterface::showMainMenu()
{
	cout << "Main menu: " << endl << endl;
	cout << "A) Create a stack... " << endl;
	cout << "B) Create a queue... " << endl;
	cout << "C) Create a binary search tree... " << endl << endl;
	cout << "Q) Exit program " << endl;
	
	cout << "What would you like to do? " << endl;
	cin >> mainOption;
}
 
void MainInterface::runSubtests()
{
	MainInterface mainControl;
 
	while ((mainOption != 'Q') && (mainOption != 'q'))
	{
		switch (mainOption)
		{
		case 'A':
		case 'a':
			StackInterface<void> myStack;
 
			myStack.displayMenu();	//Call method to display the main menu
			myStack.runTest();		//Call method to perform the tests
			break;
		case 'B':
		case 'b':
			QueueInterface<void> myQueue;
 
			myQueue.displayMenu();
			myQueue.runTest();
			break;
		case 'C':
		case 'c':	
			TreeInterface<void> myTree;
 
			myTree.displayMenu();
			myTree.runTest();
			break;
		default:
			cout << endl << "Invalid selection!" << endl;
		}//end switch
		
		system("PAUSE");	//Press any key to continue...
		system("CLS");		//Clear screen
		mainControl.showMainMenu();
		cout << "What would you like to do? " << endl;
		cin >> mainOption;
	}//end while
}
#endif
////////////////////////////////////////////////////////////
#include <iostream>
#include "MainInterface.h"
 
using namespace std;
 
int main ()
{
	MainInterface myInterface;
	
	myInterface.showMainMenu();
	myInterface.runSubtests();
 
	//Press any key to exit program
	system("PAUSE");
	return 0;
}

Open in new window

0
Comment
Question by:psycho_blood
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 17

Accepted Solution

by:
sweetfa2 earned 2000 total points
ID: 22646792
Instead of using system("CLS") place a \f at the first line of each menu

Instead of using system("PAUSE") output your own message and read in your own character.

The two iostreams are most likely the reason for your issues.
0
 

Author Comment

by:psycho_blood
ID: 22646886
OK, I stopped using the system("CLS") and I put a "\f" at the first line of each one of my menus. Now a "@" appears at the beginning of the first line. Is this the character you are talking about?
Also, if you can, try to follow these steps:
1) Select any data structure
2) Try to return to the main menu

When I do this, I have to enter the option twice. Why is that?
0
 
LVL 17

Expert Comment

by:sweetfa2
ID: 22646902
The backslash-f should put in a formfeed character which is the same as what CLS does.

Do you still have system("PAUSE") in there?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:psycho_blood
ID: 22646927
Yes, I do. Actually, I was thinking you should know I am using Visual Studio 2008 IDE, and I was thinking of using either a delay or a sleep function instead of the system("PAUSE"), is this possible?
0
 
LVL 17

Expert Comment

by:sweetfa2
ID: 22646936
You just want an any key to continue right?

just use getch() instead of the System Pause

You might want to output a line beforehand indicating that any key will continue
0
 

Author Comment

by:psycho_blood
ID: 22646945
Well, if I am not using the system("CLS") anymore, I do not need to use the system("PAUSE") neither, since now the user can scroll the window up to see what occured. With this being said, I removed both lines from my menus and I still have to enter the option in the main menu twice after returning to it. I have no idea about what is causing this.
Note: The code originally attached on my first posting is the same, just remove these both lines and try to run the program again.
0
 

Author Comment

by:psycho_blood
ID: 22647027
I got it!
I was asking for user input on both the showMainMenu() method and before the end of the while loop.
But you really helped me with your feedback! THANKS!!
0
 

Author Closing Comment

by:psycho_blood
ID: 31503278
Even when I found my mistake, you really helped me to figure it out. Thank you so much!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question