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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

771 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