Click to See Complete Forum and Search --> : C getchar()
Todd Jeffreys
May 1st, 1999, 11:34 AM
I use getchar() to get a character from STDIN in my 8080 emulation program. Thing is, i want it to return after the first character i type. It only returns after a carriage returns , which basically defeats my purposes. Does anybody know of any function that will read just one char? In assembly? C?
Roger Osborn
May 1st, 1999, 03:38 PM
Here's an example of how to do it on a console app which seems to work, although it mixes low and high level Console firkling.
#include "stdafx.h"
#include <windows.h>
#include <iostream.h>
int main(int argc, char* argv[])
{
HANDLE hIn=GetStdHandle(STD_INPUT_HANDLE);
DWORD dwMode= ENABLE_PROCESSED_INPUT ;
if(SetConsoleMode(hIn, dwMode))
{
int ch;
while(EOF!=(ch=getchar()))
{
cout << "got a " << (char)ch << endl << flush;
}
}
else
{
cerr << "Setconsole mode failed " << GetLastError() << endl;
}
return 0;
}
Nice to know the 8080 is still around. I first learnt assembly language using it 18 years ago.
Cheers,
Roger
Jason Brooks
May 1st, 1999, 04:47 PM
You save more lines of code by using _kbhit()
#include <conio.h>
#include <stdio.h>
void main( void )
{
while( !_kbhit() )
_cputs( "Hit A Key" );
printf( "\nKey struck was '%c'\n", _getch() );
_getch();
}
Well at least I used to use this in my Dos Days,
I learnt 8080, Z80, 6502 et al many years ago, when you could write games with naff graphics and sound and get paid a forutune. Now a days I just can't compete ;-)
Roger Osborn
May 1st, 1999, 05:09 PM
Good thinking. I'd forgotten about that function. Give me three or four hours and a lot of pacing up and down and I'll come up with a reason why my solution is superior...
Roger
pat
May 4th, 1999, 08:47 AM
Thanks. I had the same problem and this worked, but now, I have to hit the enter key twice for it to register. Any ideas?
Roger Osborn
May 6th, 1999, 03:06 PM
Hi Pat,
Much though I hate to admit it, I think that Jason's solution is better. Not only is it simpler, it gets enter correctly. So I'd look at using _kbhit() and _getch() if you can.
Roger
Roger Osborn
May 6th, 1999, 03:37 PM
Thinking about it you probably used my one to save CPU cycles. This seems to work:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
void main( void )
{
HANDLE hIn=GetStdHandle(STD_INPUT_HANDLE);
DWORD dwMode= ENABLE_PROCESSED_INPUT ;
if(SetConsoleMode(hIn, dwMode))
{
while(1)
{
char buf[2];
DWORD dwCount=0;
if(ReadConsole(hIn, buf, 1, &dwCount, NULL))
{
cout << "got a code " << (int)buf[0] << " "<< buf[0] << endl << flush;
}
}
}
else
{
cerr << "Setconsole mode failed " << GetLastError() << endl;
}
}
But I should check the documentation on it cos there's complications if using UNICODE.
Roger.
pat
May 6th, 1999, 03:47 PM
I've being trying getc and getchar. These didn't return until after the user pressed enter. However, I'm now just using getch all by itself and it returns per keystroke and works fine. Don't need kbhit or anything else.
thanks
pat
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.