Link to home
Start Free TrialLog in
Avatar of psycho_blood
psycho_blood

asked on

How do I fix my menus and submenus?

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

ASKER CERTIFIED SOLUTION
Avatar of sweetfa2
sweetfa2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of psycho_blood
psycho_blood

ASKER

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?
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?
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?
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
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.
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!!
Even when I found my mistake, you really helped me to figure it out. Thank you so much!