CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: C getchar()

  1. #1
    Join Date
    Apr 1999
    Posts
    396

    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?


  2. #2
    Join Date
    Apr 1999
    Posts
    48

    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





  3. #3
    Join Date
    May 1999
    Location
    UK
    Posts
    65

    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 ;-)




  4. #4
    Join Date
    Apr 1999
    Posts
    48

    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



  5. #5
    Join Date
    May 1999
    Posts
    16

    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?


  6. #6
    Join Date
    Apr 1999
    Posts
    48

    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


  7. #7
    Join Date
    Apr 1999
    Posts
    48

    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.





  8. #8
    Join Date
    May 1999
    Posts
    16

    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
  •  





Click Here to Expand Forum to Full Width

Featured