Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Finding code (c++) that enables the user to enter a menu choice without having to press the return button

Posted on 2003-03-14
Medium Priority
Last Modified: 2008-03-17

I am having problems finding code (c++) that enables the user to enter a menu choice without having to press the return button

For example
Address book          1 (choices)
Phone                2 (choices)

(Say I want to choose the phone option)

What I have
2 and the press return to goto the phone function

What I want is that user should be able to enter 2 and go straight to the phone function

DYKWIM? Feel free to email me @ busin_peo@hotmail.com
Question by:busin_peo

Expert Comment

ID: 8136029
// Get key pressed using the getch function

int Key = getch();

switch( Key )
  case '1':
  case '2':
    cout << "Invalid choise" << endl;
LVL 12

Expert Comment

ID: 8136140
Note, this is a platform issue and not a C++ issue. The problem can be solved but depends on your platform and not on the language (C++).

In windows the getch() function can be used.

In unix you can manipulate the tty so that it give you the key without waiting for return. ioctl functions to manipulate the terminal in this way exist and is easy enough.

In other platforms there are similar manners to enable reading keys from keyboard without waiting for return - but as already said, this is entirely platform dependent and has nothing to do with C++.

If this makes you wonder if you perhaps asked this question to the wrong forum the answer is "yes, you did". It has nothing to do with C++. If you go to some other forum they will probably tell you how to do it in C and the same manner can be used in C++. In fact ToHos answer above is plain C code which is also compilable in C++.


Author Comment

ID: 8157891

The above question above is an c++ question. As you see from the heading of my question "Finding code (c++) that enables the user to enter a menu choice without having to press the return button". Hence, the need for C++ code.

LVL 12

Accepted Solution

Salte earned 300 total points
ID: 8158210
Actually it isn't. The reason why it isn't is that there is simply no solution in C++ for your problem. so if you insist it is a C++ problem the simple answer is "there is no function or C++ code that does what you want to do".

Now, if you are more practical - and most people are, most people want solutions, they're not hung up in if this is a C++ solution or a non-C++ solution - then that isn't really an answer that satisfy you. So then the more elaborate answer is "There is a platform solution to your problem but not a C++ solution. Since it is a platform solution it depends on your platform and so will vary from one OS and platform to another".

ToHo's solution works fine on Windows but will not work on Unix. A similar solution that works fine on Unix but not on windows can also be provided. However, if you sit on a Macintosh neither of those solutions will help you.

So, the point is that THERE IS NO C++ CODE THAT WILL DO WHAT YOU WANT INDEPENDENT ON PLATFORM. Again you failed to tell us which platform you're on, which you must have understood by now is of absolute necessity to be able to answer your question. I interpret this to mean that you don't really want to know the answer to your question and just want to argue about things you cannot possibly get anywhere with.

I understand that you want the C++ code and most likely whatever platform you're on, if that platform support the feature of reading characters from keyboard without waiting for return - that platform will have a solution that you can use from C++. However, the truth is still that that particular solution vary from system to system and does not vary with the language. Apart from pure syntax and name of function it is exactly the same solution in Pascal or some other language if those languages also support the same feature on that same platform. So it does not depend on language, it depends on platform. The solution given above works for windows but not for Unix or Mac.

How it is possible - after this - to still insist it is a C++ question and still not tell us which platform you want this solution for - is beyond my comprehension.

I will ignore this quetion from now on.


Expert Comment

ID: 9491869
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: Salte

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question