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

  • C/C++
  • Thread starter Rocket254
  • Start date
  • Tags
    C++ Input
In summary: 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 everything that was typed in or the last character that was typed in.In summary, the assignment is to create a for loop that will continuously cycle through a set of input characters, checking for a keypress and taking an action based on the selection.
  • #1
Rocket254
33
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
  • #2
Anyone?
 
  • #3
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:
  • #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.
 
  • #5
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?
 
  • #6
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:
  • #7
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.
 
  • #8
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++
 

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

What is ANSI Std. C++ Looping Menu while checking user input?

ANSI Std. C++ Looping Menu while checking user input is a programming technique used in C++ to create a menu that allows users to make selections and continuously loop until a valid input is received. It ensures that the program does not break or crash due to incorrect inputs by repeatedly prompting the user to enter a valid choice.

How do I create a looping menu in C++?

To create a looping menu in C++, you will need to use a combination of control structures like loops and conditional statements. The menu should be contained within a loop, which will continue to run until a valid input is received. The loop should also include a condition that checks the user's input and prompts them to re-enter it if it is invalid.

What are the advantages of using ANSI Std. C++ Looping Menu while checking user input?

One of the main advantages of using ANSI Std. C++ Looping Menu while checking user input is that it ensures the program does not crash or break due to incorrect inputs. It also provides a user-friendly interface by prompting the user to re-enter their input if it is invalid, instead of abruptly terminating the program. Additionally, this technique allows for better control and organization of the menu options.

Can I customize the looping menu in C++?

Yes, you can customize the looping menu in C++ to fit your specific needs. You can change the menu options, the prompt messages, and the conditions for a valid input. This allows you to create a more personalized and user-friendly experience for your program's users.

Are there any potential issues with using ANSI Std. C++ Looping Menu while checking user input?

One potential issue with using ANSI Std. C++ Looping Menu while checking user input is that it can become repetitive and tedious for the user if they continuously enter invalid inputs. To avoid this, it is essential to provide clear instructions and prompts for the user and to anticipate potential invalid inputs in your code.

Similar threads

  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
14
Views
31K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
16
Views
4K
  • Programming and Computer Science
Replies
7
Views
8K
  • Programming and Computer Science
Replies
3
Views
4K
Back
Top