| | |
Quick question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 57
Reputation:
Solved Threads: 1
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?
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
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?
C++ Syntax (Toggle Plain Text)
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
Perhaps consider a menu using a switch case construction?
Last edited by Aia; Oct 1st, 2008 at 11:19 pm.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
•
•
Join Date: Aug 2008
Posts: 57
Reputation:
Solved Threads: 1
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.
It's not a requirement, but I'm aiming for a 100%, that'll give me a 89% in the class.
If you don't want to exit don't use the function exit() which will terminate the program.
C++ Syntax (Toggle Plain Text)
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
Last edited by Aia; Oct 2nd, 2008 at 2:07 am.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
•
•
Join Date: Mar 2008
Posts: 89
Reputation:
Solved Threads: 2
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..
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..
Bhoot
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.
•
•
•
•
another situation is obvious..which..i think you are facing..
•
•
•
•
gud luck..
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.
Last edited by Aia; Oct 2nd, 2008 at 1:02 pm.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
•
•
Join Date: Mar 2008
Posts: 89
Reputation:
Solved Threads: 2
•
•
•
•
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.
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
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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
•
•
Join Date: Aug 2008
Posts: 57
Reputation:
Solved Threads: 1
•
•
•
•
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
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)
C++ Syntax (Toggle Plain Text)
#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)
C++ Syntax (Toggle Plain Text)
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(); }
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
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.
Also you probably want to replace all the "if's" by switch statements in your display() functions.
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.
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- quick question w\ a program (Java)
- nevermind: ignore arrays problem - quick question (C++)
- A quick question about "rank" (DaniWeb Community Feedback)
- Quick question on a loop. (C++)
- A quick question (Game Development)
- quick question (C++)
- Question about 321Studios (Windows Software)
- AGP support video card question (Monitors, Displays and Video Cards)
- Quick question (Troubleshooting Dead Machines)
- Laptop LCD built into a car? (Monitors, Displays and Video Cards)
Other Threads in the C++ Forum
- Previous Thread: insert file data to linked list?
- Next Thread: linked list
Views: 857 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






