954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Quick question

Ok I'm doing my final project for my Data Structures class and have a quick question. This hasn't come up in any of my programming classes yet (which now that I think about it, is kind of odd but oh well).

What I'm having to do is write an interactive program allowing the user to access 3 different data structures. That part I have no problems with (going to use Stacks, Queues & Binary Tree). What I'm wanting is to have the main menu display:

1) Use Stacks
2) Use Queues
3) Use Binary Tree
4) Exit Program

then it goes into the submenu for that option.

What I need is to reset the program so when they finish with that data structure it brings them back to the main menu.

Here is what I'm thinking should work, am I right or do I need to go about it differently?

do
{
    " Display main menu here"

      if (option 1 is picked)
       {
           do
              {
                 "Display menu for selection"
                 run program
              }
              while (user does not select to exit)
        }

      if (option 2 is picked)
       {
          do
             {
                "same as option 1"
              }
          while (user doesnt select to exit)
       }

}
while (user doesn't select to quit entire program)


Now before everyone starts to nit pick at my code, it's just a shell to give you an idea of what I want to do.


Thanks

RayvenHawk
Junior Poster in Training
77 posts since Aug 2008
Reputation Points: 32
Solved Threads: 2
 

Perhaps consider a menu using a switch case construction?

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 
Perhaps consider a menu using a switch case construction?

Ok I decided that to organize the main menu better and make the code look nicer I went with the switch commands. But that doesn't resolve my initial problem. When I for example select stacks, I can do whatever I want within that piece of the program, but as soon as I choose to exit, it completely exits the entire program. I want it to revert back to my main menu.

It's not a requirement, but I'm aiming for a 100%, that'll give me a 89% in the class.

RayvenHawk
Junior Poster in Training
77 posts since Aug 2008
Reputation Points: 32
Solved Threads: 2
 

If you don't want to exit don't use the function exit() which will terminate the program.

sentinel set to not done
while not done:
      menu options:
      switch:
      option 1:
           do this and break
      option 2:
           do this and break
      option 3:
          I am done set sentinel to done.
continue flow of program
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

well...this thing also depends on how the menus and submenus are structured.
For example...i have faced a situation in which..i need to first select an object on which i need to perform certain operations...which are,in turn, same for all the objects..
Thus, i would definitely have a structure..in which..i have the main menu which gives options to select an object..and then i move to another menu (which i may/ may not consider submenu) which gives choices for the operations.. :)

another situation is obvious..which..i think you are facing..

anyways..i have attached a program which deals with both the situations..
have a look on it if u want to. :)
NOTE : its a CPP (and hence an OO) program..Also the graphics.h header file of Borland Turbo C++ i have used is very much primitive..so i think the code wont work..unless you use Turbo C++...
however..i dont think thats your concern...so hope this helps you.. :)
gud luck..

Attachments 2DTRANS.CPP (18.75KB)
bhoot_jb
Junior Poster in Training
89 posts since Mar 2008
Reputation Points: 57
Solved Threads: 2
 

A comment if I may bhoot_jb.

another situation is obvious..which..i think you are facing..


You make heavy use of [..]. I know what ellipsis are [...], but I can't figure what's the process that warrant the use of two consecutive periods.gud luck..
What's the advantage of misspelling "good" to just save the writing of one more vowel?
Why not "gu luc"? That seems to me more advantageous. Better yet, "guluc" will save even the space.
Of course, all is for naught because of the mysterious two periods afterward.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

A comment if I may bhoot_jb.

You make heavy use of [..]. I know what ellipsis are [...], but I can't figure what's the process that warrant the use of two consecutive periods.

What's the advantage of misspelling "good" to just save the writing of one more vowel? Why not "gu luc"? That seems to me more advantageous. Better yet, "guluc" will save even the space. Of course, all is for naught because of the mysterious two periods afterward.

lolz. OK. thanks for pointing out my mistakes. Actually i was used to such typing in this weird way in mobile chatting. :) There is nothing special about using two consecutive periods or ellipsis. :)
And as far as "gud luck" is concerned, its almost the same issue, i.e., i make a heavy use of short spellings. :| i have been warned earlier against using such short spellings.
However i have improved much better and will do the same in future. :)
Now, this is going off-topic. So i better conclude. :)

bhoot_jb
Junior Poster in Training
89 posts since Mar 2008
Reputation Points: 57
Solved Threads: 2
 

There's a few solutions to that 'problem'. I usually try and keep the clutter out of main by putting all the meat and bones of main() into functions and only using main to call them, and potentially have a loop (which would return you to the first menu for example). This, however, is a bit worse for resources than using a switch statement with all the code inside of each case. I, personally, would be more conscerned with getting the binary tree working :P

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 
There's a few solutions to that 'problem'. I usually try and keep the clutter out of main by putting all the meat and bones of main() into functions and only using main to call them, and potentially have a loop (which would return you to the first menu for example). This, however, is a bit worse for resources than using a switch statement with all the code inside of each case. I, personally, would be more conscerned with getting the binary tree working :P


Actually all of my data structures function in their seperate programs (including the binary tree). I created a pointer to the submenu on main within the switch statements: i.e

switch (m_select)
case 1:
{
stackmenu();
break;
}

I ran the program and the stack program is complete and functions correctly (except when I exit, it proceeds to crash, but thats not important at the moment).

So if I call the menus up within the switch statements how would I get those sub menus to terminate and return to the actual main menu?

Here is my main menu code (seeing what I have may help)

#include "binaryTreeSearch.h"
#include "mystack.h"
#include "queuetype.h"
#include "menu_display.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int m_select;
	menu_display menu;

	cout << "Final Project Program" << endl;
	cout << endl;
	cout << "1) Stack Data Structures" << endl;
	cout << "2) Queue Data Structures" << endl;
	cout << "3) Binary Tree Data Structures" << endl;
	cout << "4) End Program" << endl;
	cout << endl;
	cout << "Please make selection: ";
	cin >> m_select;
	
	switch (m_select)
	{
	case 1:
		menu.s_display();
	break;

	case 2:
		menu.q_display();
	break;

	case 3:
		menu.b_display();
	break;

	case 4:
		cout << "Now exiting program.\n";
	break;

	default:
		cout << "Invalid selection.\n";
	break;
	}


}


And here is my stack program (some of it)

void menu_display::s_display()
{
	int select;
	
	//generate main welcome screen
	cout << "Welcome to my stack program. "<<endl;
	cout << "Please select what type of stack you want. "<<endl;
	cout << "1 - Int stack" <<endl;
	cout << "2 - String stack" <<endl;
	cout << "3 - Quit Program" <<endl;
	cin >> select; //user selects option

	if (select == 1)
	{
		int amount;
		int num;
		int choice;
		int j = 0;

		cout << "How many INT stacks do you want? ";
		cin >> amount;
		//create int stack (1 original, 1 for copy & 1 to work with)
		mystack<int> intStack(amount);
		mystack<int> copyStack(amount);
		mystack<int> workStack(amount);

		intStack.initializeStack(); //set original stack to 0
		cout <<endl;
	do
	{
		//prompt for functions to do with the stack
		cout << "Make selection." <<endl;
		cout << "1 - Add to stack." <<endl;
		cout << "2 - Display top of stack." <<endl;
		cout << "3 - Remove item from stack." <<endl;
		cout << "4 - Copy stack." <<endl;
		cout << "5 - Quit Program." <<endl;
		cin >> choice;

		if (choice == 1)
		{
			//add items to stack until "j" reaches amount user previously inputted
			cout << "Input your values pressing Enter after each one" <<endl;

			do
			{
				cin >> num;
				intStack.push(num);
				j++;
			}
			while(j < amount);
			workStack = intStack;
		}

		if (choice == 2)
		{
			//display contents of stack until end is reached
			copyStack = intStack;
			cout << "The stack elements are: ";
			while(!copyStack.isEmptyStack())
			{
				cout<< copyStack.top() <<" ";
				copyStack.pop();
			}
			cout << endl;
		}

		if (choice == 3)
		{
			//remove top item from stack
			cout << "Removing top element.";
			intStack.pop();
		}

		if (choice == 4)
		{
			//display content of original stack until end is reached
			copyStack = workStack;
			cout << "The original stack is: ";
			while(!workStack.isEmptyStack())
			{			
				cout << workStack.top() <<" ";
				workStack.pop();
			}
			cout << endl;
			//display content of copied stack until end is reached
			cout << "The copied stack is: ";
			while(!copyStack.isEmptyStack())
			{
				cout << copyStack.top() <<" ";
				copyStack.pop();
			}
			cout << endl;
		}

	}
	while(choice != 5); //keep displaying menu options until "5" is selected
	//clear memory used by stacks
	intStack.~mystack();
	copyStack.~mystack();
	workStack.~mystack();

	}
RayvenHawk
Junior Poster in Training
77 posts since Aug 2008
Reputation Points: 32
Solved Threads: 2
 

I am going to try to answer this, but I don't completely understand your problem.

Your function s_display() will process the stack routine and then return back to the switch statement in your main(). If you want to continue asking the user to process a different data structure type after processing the stack, you need something like this.

int main()
{
  int m_select;
  bool done = false;

  while (!done){

    cout << "Final Project Program" << endl;
    cout << endl;
    cout << "1) Stack Data Structures" << endl;
    cout << "2) Queue Data Structures" << endl;
    cout << "3) Binary Tree Data Structures" << endl;
    cout << "4) End Program" << endl;
    cout << endl;
    cout << "Please make selection: ";
    cin >> m_select;

    switch (m_select)
    {
    case 1:
      cout << "calling stack.\n";
      s_display();
      break;

    case 2:
      cout << "calling queue \n"; 
      menu.q_display();
      break;

    case 3:
      cout << "calling bintree \n"; 
      menu.b_display();
      break;

    case 4:
      done = true;
      cout << "Now exiting program.\n";
      break;

    default:
      done = true;
      cout << "Invalid selection.\n";
      break;
    }
  }

}


Also you probably want to replace all the "if's" by switch statements in your display() functions.

stilllearning
Posting Whiz
309 posts since Oct 2007
Reputation Points: 161
Solved Threads: 43
 

I'll clean up the code tomorrow, and I'll try the while command. I was thinking of the do/while but am unsure if while in the sub-menus if they'll quit back to the main one or not. I'll update tomorrow when I try it.

RayvenHawk
Junior Poster in Training
77 posts since Aug 2008
Reputation Points: 32
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You