|
-
May 1st, 1999, 11:34 AM
#1
C getchar()
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?
-
May 1st, 1999, 03:38 PM
#2
Re: C getchar()
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
-
May 1st, 1999, 04:47 PM
#3
Re: C getchar()
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 ;-)
-
May 1st, 1999, 05:09 PM
#4
Re: C getchar()
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
-
May 4th, 1999, 08:47 AM
#5
Re: C getchar()
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?
-
May 6th, 1999, 03:06 PM
#6
Re: C getchar()
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
-
May 6th, 1999, 03:37 PM
#7
Re: C getchar()
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.
-
May 6th, 1999, 03:47 PM
#8
Re: C getchar()
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|