Quick question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2008
Posts: 57
Reputation: RayvenHawk is an unknown quantity at this point 
Solved Threads: 1
RayvenHawk RayvenHawk is offline Offline
Junior Poster in Training

Quick question

 
0
  #1
Oct 1st, 2008
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?

  1.  
  2. do
  3. {
  4. " Display main menu here"
  5.  
  6. if (option 1 is picked)
  7. {
  8. do
  9. {
  10. "Display menu for selection"
  11. run program
  12. }
  13. while (user does not select to exit)
  14. }
  15.  
  16. if (option 2 is picked)
  17. {
  18. do
  19. {
  20. "same as option 1"
  21. }
  22. while (user doesnt select to exit)
  23. }
  24.  
  25. }
  26. while (user doesn't select to quit entire program)
  27.  

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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Quick question

 
0
  #2
Oct 1st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 57
Reputation: RayvenHawk is an unknown quantity at this point 
Solved Threads: 1
RayvenHawk RayvenHawk is offline Offline
Junior Poster in Training

Re: Quick question

 
0
  #3
Oct 2nd, 2008
Originally Posted by Aia View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Quick question

 
0
  #4
Oct 2nd, 2008
If you don't want to exit don't use the function exit() which will terminate the program.

  1. sentinel set to not done
  2. while not done:
  3. menu options:
  4. switch:
  5. option 1:
  6. do this and break
  7. option 2:
  8. do this and break
  9. option 3:
  10. I am done set sentinel to done.
  11. 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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 89
Reputation: bhoot_jb is on a distinguished road 
Solved Threads: 2
bhoot_jb bhoot_jb is offline Offline
Junior Poster in Training

Re: Quick question

 
0
  #5
Oct 2nd, 2008
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..
Attached Files
File Type: cpp 2DTRANS.CPP (18.8 KB, 2 views)
Bhoot
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Quick question

 
0
  #6
Oct 2nd, 2008
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.
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 89
Reputation: bhoot_jb is on a distinguished road 
Solved Threads: 2
bhoot_jb bhoot_jb is offline Offline
Junior Poster in Training

Re: Quick question

 
1
  #7
Oct 2nd, 2008
Originally Posted by Aia View Post
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Quick question

 
0
  #8
Oct 2nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 57
Reputation: RayvenHawk is an unknown quantity at this point 
Solved Threads: 1
RayvenHawk RayvenHawk is offline Offline
Junior Poster in Training

Re: Quick question

 
0
  #9
Oct 2nd, 2008
Originally Posted by skatamatic View Post
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)
  1. #include "binaryTreeSearch.h"
  2. #include "mystack.h"
  3. #include "queuetype.h"
  4. #include "menu_display.h"
  5. #include <iostream>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. int m_select;
  13. menu_display menu;
  14.  
  15. cout << "Final Project Program" << endl;
  16. cout << endl;
  17. cout << "1) Stack Data Structures" << endl;
  18. cout << "2) Queue Data Structures" << endl;
  19. cout << "3) Binary Tree Data Structures" << endl;
  20. cout << "4) End Program" << endl;
  21. cout << endl;
  22. cout << "Please make selection: ";
  23. cin >> m_select;
  24.  
  25. switch (m_select)
  26. {
  27. case 1:
  28. menu.s_display();
  29. break;
  30.  
  31. case 2:
  32. menu.q_display();
  33. break;
  34.  
  35. case 3:
  36. menu.b_display();
  37. break;
  38.  
  39. case 4:
  40. cout << "Now exiting program.\n";
  41. break;
  42.  
  43. default:
  44. cout << "Invalid selection.\n";
  45. break;
  46. }
  47.  
  48.  
  49. }

And here is my stack program (some of it)
  1. void menu_display::s_display()
  2. {
  3. int select;
  4.  
  5. //generate main welcome screen
  6. cout << "Welcome to my stack program. "<<endl;
  7. cout << "Please select what type of stack you want. "<<endl;
  8. cout << "1 - Int stack" <<endl;
  9. cout << "2 - String stack" <<endl;
  10. cout << "3 - Quit Program" <<endl;
  11. cin >> select; //user selects option
  12.  
  13. if (select == 1)
  14. {
  15. int amount;
  16. int num;
  17. int choice;
  18. int j = 0;
  19.  
  20. cout << "How many INT stacks do you want? ";
  21. cin >> amount;
  22. //create int stack (1 original, 1 for copy & 1 to work with)
  23. mystack<int> intStack(amount);
  24. mystack<int> copyStack(amount);
  25. mystack<int> workStack(amount);
  26.  
  27. intStack.initializeStack(); //set original stack to 0
  28. cout <<endl;
  29. do
  30. {
  31. //prompt for functions to do with the stack
  32. cout << "Make selection." <<endl;
  33. cout << "1 - Add to stack." <<endl;
  34. cout << "2 - Display top of stack." <<endl;
  35. cout << "3 - Remove item from stack." <<endl;
  36. cout << "4 - Copy stack." <<endl;
  37. cout << "5 - Quit Program." <<endl;
  38. cin >> choice;
  39.  
  40. if (choice == 1)
  41. {
  42. //add items to stack until "j" reaches amount user previously inputted
  43. cout << "Input your values pressing Enter after each one" <<endl;
  44.  
  45. do
  46. {
  47. cin >> num;
  48. intStack.push(num);
  49. j++;
  50. }
  51. while(j < amount);
  52. workStack = intStack;
  53. }
  54.  
  55. if (choice == 2)
  56. {
  57. //display contents of stack until end is reached
  58. copyStack = intStack;
  59. cout << "The stack elements are: ";
  60. while(!copyStack.isEmptyStack())
  61. {
  62. cout<< copyStack.top() <<" ";
  63. copyStack.pop();
  64. }
  65. cout << endl;
  66. }
  67.  
  68. if (choice == 3)
  69. {
  70. //remove top item from stack
  71. cout << "Removing top element.";
  72. intStack.pop();
  73. }
  74.  
  75. if (choice == 4)
  76. {
  77. //display content of original stack until end is reached
  78. copyStack = workStack;
  79. cout << "The original stack is: ";
  80. while(!workStack.isEmptyStack())
  81. {
  82. cout << workStack.top() <<" ";
  83. workStack.pop();
  84. }
  85. cout << endl;
  86. //display content of copied stack until end is reached
  87. cout << "The copied stack is: ";
  88. while(!copyStack.isEmptyStack())
  89. {
  90. cout << copyStack.top() <<" ";
  91. copyStack.pop();
  92. }
  93. cout << endl;
  94. }
  95.  
  96. }
  97. while(choice != 5); //keep displaying menu options until "5" is selected
  98. //clear memory used by stacks
  99. intStack.~mystack();
  100. copyStack.~mystack();
  101. workStack.~mystack();
  102.  
  103. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Quick question

 
0
  #10
Oct 2nd, 2008
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.

  1. int main()
  2. {
  3. int m_select;
  4. bool done = false;
  5.  
  6. while (!done){
  7.  
  8. cout << "Final Project Program" << endl;
  9. cout << endl;
  10. cout << "1) Stack Data Structures" << endl;
  11. cout << "2) Queue Data Structures" << endl;
  12. cout << "3) Binary Tree Data Structures" << endl;
  13. cout << "4) End Program" << endl;
  14. cout << endl;
  15. cout << "Please make selection: ";
  16. cin >> m_select;
  17.  
  18. switch (m_select)
  19. {
  20. case 1:
  21. cout << "calling stack.\n";
  22. s_display();
  23. break;
  24.  
  25. case 2:
  26. cout << "calling queue \n";
  27. menu.q_display();
  28. break;
  29.  
  30. case 3:
  31. cout << "calling bintree \n";
  32. menu.b_display();
  33. break;
  34.  
  35. case 4:
  36. done = true;
  37. cout << "Now exiting program.\n";
  38. break;
  39.  
  40. default:
  41. done = true;
  42. cout << "Invalid selection.\n";
  43. break;
  44. }
  45. }
  46.  
  47. }

Also you probably want to replace all the "if's" by switch statements in your display() functions.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 857 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC