Solved

retrieving a single character in VC++

Posted on 2000-03-25
2
182 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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.

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now