Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prevent character display on user input (std::cin)

Status
Not open for further replies.

Safacto

Programmer
Feb 10, 2002
15
0
0
GB
Hi,

Just a quick question.

If I request the user to enter an integer using a Win32 console (for example), is there a way to prevent any characters other than numbers from being displayed on screen.

Is there a way to disable the entry of specific ASCI characters or something??

I realise there are the clear() and ignore() commands, but these only work after the enter key is pressed. Is there a way to actively stop "wrong" characters being input??

Cheers,
Richard.
 
Your best bet is to input everything into a string, then check the string for non-numeric characters.
James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Yeh, I thought of that, but it doesn't prevent the standard display of input in the console window.

I need a way of being able to "check" each character at the moment it is input, without the user having to press "Enter" after each character.

All this could be solved by using a GUI with a text box, but I don't want to do that.
 
If you are not going to use a GUI then look at getc in the stdio.h file. This will return the character read from a stream after converting it to a integer (without the sign extension). On an error or end of file it returns EOF. The following is from Borland's help file:
Code:
#include <stdio.h>

int main(void)
{
   char ch;

   printf(&quot;Input a character:&quot;);
/* read a character from the standard input stream */
   ch = getc(stdin);
   printf(&quot;The character input was: '%c'\n&quot;, ch);
   return 0;
}
This should work under DOS, Unix, Windows, or Ansi C. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top