473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Calling main menu is displaying sub menu

i am doing an airplain seat booking system. Here I have a main menu of chooseflight() and submenu of printmenu(). Whenever I press 'f' from printmenu's options I should return to mainmenu. But I cannot. Please help me!

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     chooseflight();
  4.     fflush(stdin);
  5.     return 0;
  6. }
  7.  
  8. void chooseflight(void)
  9. {
  10.     char selectflight;
  11.     printf("a) Flight 102 b) Flight 311 c) Flight 444 d) Flight 519 e)Quitprogram\n");
  12.         scanf("%c",&selectflight);
  13.         switch(selectflight)
  14.         {
  15.             case 'a':
  16.              puts("Welcome to flight 102 service");
  17.              while(1){
  18.              printmenu102();
  19.              }
  20.              break;
  21.              case 'b':
  22.              puts("Welcome to flight 311 service");
  23.              while(1){
  24.              printmenu311();
  25.              break;
  26.              }
  27.              case 'c':
  28.              puts("Welcome to flight 444 service");
  29.              while(1){
  30.              printmenu444();
  31.              }
  32.              break;
  33.              case 'd':
  34.              puts("Welcome to flight 519 service");
  35.              while(1){
  36.              printmenu519();
  37.              }
  38.              break;
  39.              case 'e':
  40.              Quitprogram();
  41.              break;
  42.         }
  43. }
  44.  
  45. void printmenu102()
  46. {
  47.         char lablename;
  48.  
  49.     printf("a) Show number of empty seats b) Show list of empty seats c)Show alphabetical list of seats d) Assign a passenger toa seat e)Delete a seat assignment f) Quittotopmenu\n");
  50.  
  51.     scanf("%c",&lablename);
  52.     switch(lablename)
  53.     {
  54.         case 'a':
  55.             Noofemptyseats102();
  56.             break;
  57.             case 'b':
  58.             Listofemptyseats102();
  59.             break;
  60.             case 'c':
  61.             Alphabeticallistofseats102();
  62.             break;
  63.             case 'd':
  64.             Assingseats102();
  65.             break;
  66.             case 'e':
  67.             Deleteseats102();
  68.             break;
  69.             case 'f':
  70.             Quittotopmenu();
  71.             break;
  72.     }
  73. }
  74.  
  75. void Quitprogram(void)
  76.  {
  77.  exit(EXIT_FAILURE);
  78.  }
  79.  
  80.  void Quittotopmenu(void)
  81.  {
  82. chooseflight();
  83.  }
My output is:
a) Flight 102 b) Flight 311 c) Flight 444 d) Flight 519 e)Quitprogram

a

Welcome to flight 102 service

a) Show number of empty seats b) Show list of empty seats c)Show alphabetical list of seats d) Assign a passenger toa seat e)Delete a seat assignment f) Quittotopmenu

a) Show number of empty seats b) Show list of empty seats c)Show alphabetical list of seats d) Assign a passenger toa seat e)Delete a seat assignment f) Quittotopmenu

f

a) Flight 102 b) Flight 311 c) Flight 444 d) Flight 519 e)Quitprogram

a) Show number of empty seats b) Show list of empty seats c)Show alphabetical list of seats d) Assign a passenger toa seat e)Delete a seat assignment f) Quittotopmenu
Aug 17 '14 #1

✓ answered by weaknessforcats

It looks like you are managing these menus by having chooseflight call itself.

I suggest you handle the top menu like:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  
  4.    while(1)
  5.    {
  6.       switch(choice)
  7.       {
  8.          case 'a':
  9.               callchoicea();
  10.        }      break;
  11.      }
  12.  
  13. }
  14.  
When callchoicea() returns, that choice is done.

The fact that callchoicea() many have sub menus does not alter the fact that when it returns, the choice is done. So to get to the top menu, you just return in callchoicea().

This avoids recursion (having the function call itself) and keeps the top menu inside one infinite while loop.

callchoicea() would follow the same logic pattern as main().

3 2448
weaknessforcats
9,208 Expert Mod 8TB
It looks like you are managing these menus by having chooseflight call itself.

I suggest you handle the top menu like:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  
  4.    while(1)
  5.    {
  6.       switch(choice)
  7.       {
  8.          case 'a':
  9.               callchoicea();
  10.        }      break;
  11.      }
  12.  
  13. }
  14.  
When callchoicea() returns, that choice is done.

The fact that callchoicea() many have sub menus does not alter the fact that when it returns, the choice is done. So to get to the top menu, you just return in callchoicea().

This avoids recursion (having the function call itself) and keeps the top menu inside one infinite while loop.

callchoicea() would follow the same logic pattern as main().
Aug 17 '14 #2
Thanks bt somehow I solved it yesterday night and did samething like you! :)
Aug 18 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
Another trick is to have your menu programs communicate by using menu choices that are not entered by the user. The menu function can return a choice that the upper menu can service even though it is not entered by the user. A good example is to abort the program or to signal database repair without actually calling the repair function.

Look up the Finite State Machine.
Aug 19 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: vikram.cvk | last post by:
Hello Experts, Im trying to design a CSS vertical drop down menu which should have the following functionality. Home About Us | -->Overview
1
by: Peter | last post by:
I found this awesome example of calling a Main Thread from a worker thread on abstractvb.com but I read through the example and would like to see if anyone can clarify some of this code below: ...
1
by: Chad Miller | last post by:
I know haw to call a the main thread from a worker thread in a windows form class, but how does a worker thread call its main thread from a none windows form class? Thank You. ...
3
by: kaustubh | last post by:
I'm using Borland C++ ver 5.02 There's a problem when i try to call main() from within the main It says "cannot call main from within the program" I'm using *.c as the default extension.
6
by: JJ | last post by:
Question: Which controls to use as menus: ============================= I must be approaching this the wrong way, as it clearly must be possible. I am writing a very basic CMS. At the top of...
1
by: proplady | last post by:
I've had this menu for some time, and periodically make changes. I've made some changes now (just some menu items), and the menu will not show up on my webpages. I know it's something simple,...
1
by: kiran520 | last post by:
Hi Iam having problem while displaying menu in ie7. Code for displaying menu is function mmLoadMenus() { if (window.mm_menu_0308002013_0) return; window.mm_menu_0308002013_0...
4
by: PianoMan64 | last post by:
Hey Experts, I'm not sure how to get around this, and any help would be most greatfull. I have an issue with having a CSS menu that displays correctly in IE and not correctly in Firefox 3.0.3,...
1
by: fonix232 | last post by:
Hey it's me again :) I will have some more noobish questions today, so prepare yourselves! So here you go: In my app when I close a form, I want a menu update script to be ran, because on the...
2
colinod
by: colinod | last post by:
I have built a Jquery horizontal sub menu which gets data from asp and works fine in all browsers apart from IE, in ie when you hover over main buttons sub menu appears and when you move uver the sub...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.