Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

retrieving a single character in VC++

Posted on 2000-03-25
2
Medium Priority
?
191 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 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

916 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