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
SUMMARY

The discussion focuses on creating a Win32 console application using ANSI Standard C++ that displays a looping menu while checking for user input. The main challenge is implementing non-blocking input without using non-ANSI functions like getch(). Suggestions include utilizing switch statements for menu options and exploring ANSI C functions such as getchar() and putchar(). The consensus is that achieving a non-blocking input mechanism within ANSI compliance is complex, as standard input functions typically block execution until input is received.

PREREQUISITES
  • Understanding of ANSI Standard C++ syntax and structure
  • Familiarity with control structures, specifically switch statements
  • Knowledge of ANSI C input functions like getchar() and putchar()
  • Basic concepts of looping constructs in programming
NEXT STEPS
  • Research ANSI C input functions and their behavior, focusing on getchar() and putchar()
  • Explore methods to implement non-blocking input in C++ applications
  • Investigate the use of threading in C++ for handling concurrent tasks
  • Learn about polling techniques for input handling in console applications
USEFUL FOR

Developers working on console applications in C++, particularly those needing to implement user-interactive menus while adhering to ANSI standards.

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
34K
  • · 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
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 4 ·
Replies
4
Views
6K