Solved

retrieving a single character in VC++

Posted on 2000-03-25
2
183 Views
Last Modified: 2010-04-02
I'm presently using MS Visual C++ 6 and I would like to know the best way to input a single character (with no need for the user to press enter).  I've tried using the _getch, which works, but it does not display my prompt until after the _getch function is executed(the program sits there with a blank screen waiting for input).

Then if I ask the user for more input the inputted character is echoed after the prompt.

eg:
cout<<"Enter a character: ";
ch = _getch();
cout<<ch<<endl;
cout<<"Enter an integer: ";
cin>>x;

upon execution:
(screen is blank at this point waiting for input even though the prompt should be there....so I press a character then the following is displayed)
Enter a character: g

Enter an integer: g


I understand it's a stream related problem, but I have no idea what's going on in the streams.

Thanks in advance,

G
0
Comment
Question by:h_j_s
2 Comments
 
LVL 5

Expert Comment

by:pitonyak
ID: 2666573
The problem is buffered IO. flush the buffer... with no docs I can but guess, but I think that you need to do

cout << "text: ";
cout.flush();

when you perform

cout << endl;

This also causes a flush to happen but not what you desire...

andy
0
 
LVL 1

Accepted Solution

by:
jbodom earned 50 total points
ID: 2692443
Use printf() and getche() like:

char ch;
printf("Enter a character: ");
ch=getche();

getche() echoes the character to the screen without having to press return.



0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

776 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