Thread: Going back to a main menu from anywhere

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    207

    Question Going back to a main menu from anywhere

    I have various menus in my program, and I would like the user to be able to press for example the key "ESC" at any moment so that he'd return to the main menu. I would know how to include this option in every menu (just adding one option at the end), but I want to be able to go back to the main menu at any moment. For example, if the user is entering new data for a new project, but he changes his mind, now he'd have to finish writing data (even if it useless).

    I've had a look thgough the two books I have, and through the help file from the compiler (Turbo C++ 3.0) but I couldn't find anything.

    It's not homeworking, just curiosity

    Any simple ideas?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    Well, that depends upon how the user enters data. If you are using a "high level" function like scanf() for this, then your options are quite limited and relatively complex. If however, you are doing your own string parsing (you are getting each and every individual key) then you simply put in an if testing for ASCII 27 (ESC) or whatever. I bet that doesn't help you much, because making your own scanf() is fairly complex too, but that's what I would do.
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    well, if you find out the scan code for something like F1 (you wouldn't want ESC) then you can use goto...I know it's not great programming proactice, but it's probably the simplest solution typing-wise.

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    I tried something like this once, what I did was to have the main menu at the beginning of main() then when I wanted to return to the main menu from deep inside the program I just called main()
    Code:
    if( ch == ESC)
         main();
    This worked, but with some anomilies, like next time through it skipped part of the prog, I think this may have been due to data left in the prog somewhere(which I couldn't find). If you try this method let me know how you get on as I'd be interested to hear if you have the same problem.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll need to catch keystrokes. Basicly, you'll have to handle input a character at a time, and when they hit a "hotkey" or "enter", you invoke the correct command.

    Something like:
    Code:
    c = getkeyfromuser();
    switch(c)
    {
       case ESCAPE: {flushbuffer( buffer ); mainmenu( ); break; }
       case ...
       default: addctobuffer(c, buffer);
    }
    You actually want a finite state machine. This is to say: you run a switch / loop around your whole program, and track the current "state". The state is used as the switch/case trigger, invoking the correct reaction. This is as I basicly described it above.

    By the way, escape I believe is in decimal, 27. The keystroke for it may or may not be the same. At any rate, you have to monitor keystrokes, not line reads.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    Let's see if I understand you. For example I have my main like this:

    int
    main(void)
    {


    MENU();

    return (0);
    }


    So that thgrough the MENU() function I get to the program. If I include this line into a loop as quzah has described, would the program be "alert" waiting for the hotkey to be pressed while it's running?

    I think I'll try to make a little program to check, so I'll post it tomorrow morning.

    Thanks for the effort guys,

    Well, I've been trying this morning to come up with something, but I can't get anything to work. I think it might be too much for me, so I'll keep on with simple functions by now.
    Last edited by Gades; 11-14-2001 at 07:30 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused as to how to pass back to main
    By J-Camz in forum C Programming
    Replies: 6
    Last Post: 11-28-2008, 07:21 AM
  2. Passing a memory location back to main
    By jbsloan in forum C Programming
    Replies: 5
    Last Post: 03-07-2005, 12:17 PM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  4. Beautifying a main menu screen
    By fkheng in forum C Programming
    Replies: 2
    Last Post: 07-03-2003, 06:29 AM
  5. quick question about C Dos text menu pgm i was doing
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 09-16-2001, 10:26 AM