Solved

retrieving a single character in VC++

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

758 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

18 Experts available now in Live!

Get 1:1 Help Now