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
Solved

retrieving a single character in VC++

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

856 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