Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

ANSI Std. C++ Looping Menu while checking user input

  1. Jan 14, 2010 #1
    I am trying to solve a little problem that I have. I need to create a simple win32 console app that displays a multiple option menu on a x second loop. That part is easy. Where I am getting stuck is this: While the app is looping outputting the menu, it also needs to be able to check for a character input to indicate which menu option is pressed, exit the loop, and take the appropriate action according to the menu option selected.

    If managed to accomplish this (somewhat) by using kbhit()

    http://pastebin.com/m63cf5957 [Broken]

    The only problem left, is the fact that the must be developed using only ANSI Standard C++ code with no precompiled headers.

    Any ideas? I'm stumped without using non-ANSI code. It has been a while since I've had to do a simple command line app and back then I only had simple menus that would run once, allow the user to select an option and take action. After the action, the menu would be shown again.

    Thanks in advance for any and all help.

    Last edited by a moderator: May 4, 2017
  2. jcsd
  3. Jan 16, 2010 #2
  4. Jan 16, 2010 #3
    K&R has a solution for this in one of their chapters, and I'm pretty sure ANSI C is compatible with ANSI C++.
    Switch statements are one of those awesome control structures perfectly suited for menus. I think there's also a way to do switch statements with characters.
    Code (Text):
    case 'A':
    case 'B':
    a = "no choice"
    Switch statements drop through, so if neither case is hit, the default happens.

    For the input, go old school and look up getchar() and putchar() and all the other ANSI C stdin functions. They're open source, so you may just have to include the functions (with proper attribution) in your own code. You may have to do some input processing on the characters.

    As for the rest: I don't think an infinite loop is the best solution. You want the loop to continue only when a specific case in your switch statement happens, (basically only when some condition is or isn't met.) Structure your while loop around that.
    Last edited: Jan 16, 2010
  5. Jan 16, 2010 #4
    As far as I know, this can't be done. The issue is basically that none of the ANSI C input functions are guaranteed to be non-blocking. Most functions (getch(), etc.) will stop and wait for input rather than return an error code. You have to allow non-ANSI functions or redesign your program.
  6. Jan 17, 2010 #5
    If I use getchar() or putchar() before the switch, won't the program stop and wait on that line until I actually input from the keyboard?

    I need the menu to loop continuously every 10 seconds until I choose an option. For example, if I do nothing after loading the program for 30 sec. the menu will print to the screen 3 times.

    I came up with a solution that does what I need but from what I read online it is not ANSI compliant.

    Code (Text):

    #include <conio.h>
    #include <iostream>

    using namespace std;

    int main()
        for(int i=0; i < 10000; i++)

                // Do switch on char input here
            //Print Menu
    Any ideas how to recreate this so that it is ANSI compliant?
  7. Jan 17, 2010 #6
    So force an error code, catch the exception, and throw in a timer. Though it looks like good old cin may work fine for this task, you just have to figure out how to inject automatic input once your time limit has expired. Have you tried putting cin into a for loop and seeing what happens when the time expires?
    What is this for? This assignment seems geared towards you writing a for loop that basically goes:
    TIME = //some formula that's basically (# of processor loops per instructions * processor time/per second) * interrupt seconds
    for (i=0;i<TIME; i++):
    some function to get stuff off the buffer(), like some of the scan() function
    then you filter the input buffer, which will either have your input or won't. (As long as you don't flush the buffer, it'll always have something on it.

    I found some solutions that basically rewrites cin, which I think is the direction you have to go in.
    Last edited: Jan 17, 2010
  8. Jan 17, 2010 #7

    Thanks for the link. Though, is any of the solutions found there actually ANSI standard. I figure the threading solution is but I'm not sure about the others.
  9. Jan 17, 2010 #8
    I don't think the threaded solution is 'cause I don't think the boost libraries are part of the ANSI C package, though pthread might be.
    The other two solutions may work just fine, I just don't know off hand what's included in ANSI C. You've gotta root around in the libraries and find the functions you need. (polling looks about right.)

    throwing ANSI C++ into google pulls up some great resources, like:
    C reference library
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook