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

  • Context: C/C++ 
  • Thread starter Thread starter Rocket254
  • Start date Start date
  • Tags Tags
    C++ Input
Click For Summary

Discussion Overview

The discussion revolves around creating a Win32 console application that displays a looping menu while simultaneously checking for user input to select menu options. The challenge is to implement this using only ANSI Standard C++ without relying on non-ANSI functions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their attempt to create a looping menu that checks for character input using kbhit(), but notes the requirement to adhere to ANSI Standard C++.
  • Another participant suggests using switch statements for menu options and mentions that ANSI C is compatible with ANSI C++. They propose using getchar() and putchar() for input, but acknowledge that these functions may block execution.
  • A different participant argues that ANSI C input functions are not guaranteed to be non-blocking, suggesting that non-ANSI functions may be necessary or that the program needs redesigning.
  • One participant expresses concern that using getchar() or putchar() would halt the program until input is received, which contradicts the need for a continuous menu loop.
  • Another participant proposes a workaround involving a timer and filtering the input buffer, suggesting that cin might be adapted for this purpose.
  • There is mention of solutions that rewrite cin, with questions about their compliance with ANSI standards, particularly regarding threading solutions and the use of libraries like Boost.

Areas of Agreement / Disagreement

Participants express differing views on the feasibility of implementing a non-blocking input method using only ANSI Standard C++. There is no consensus on whether a solution exists that meets all requirements without using non-ANSI functions.

Contextual Notes

Participants highlight limitations related to ANSI compliance and the blocking nature of standard input functions. There are unresolved questions about the compatibility of certain libraries and functions with ANSI standards.

Who May Find This Useful

Developers working on console applications in C++ who are interested in menu implementation and user input handling, particularly within the constraints of ANSI compliance.

Rocket254
Messages
33
Reaction score
0
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


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.

-G
 
Last edited by a moderator:
Technology news on Phys.org
Anyone?
 
Rocket254 said:
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.

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:
switch(c){
case 'A':
case 'B':
default:
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:
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.
 
story645 said:
For the input, go old school and look up getchar() and putchar()

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:
#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    while(1)
  {
    for(int i=0; i < 10000; i++)
    {
        Sleep(1);

        if(kbhit())
        {
            // Do switch on char input here
        }
    }
        //Print Menu
  }
 }
}

Any ideas how to recreate this so that it is ANSI compliant?
 
hamster143 said:
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.
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:
story645 said:
I found some solutions that basically rewrites cin, which I think is the direction you have to go in.


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.
 
Rocket254 said:
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.

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 got to 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
C++
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 14 ·
Replies
14
Views
35K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
18
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 4 ·
Replies
4
Views
6K