Thread: REPLACING for getch?

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    71

    REPLACING for getch?

    I want a function like getch ()

    I know conio.h it's not a standard header and why I want another function

    sorry for mistakes(I'm not English)->google translate

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by nutzu2010 View Post
    I want a function like getch ()

    I know conio.h it's not a standard header and why I want another function

    sorry for mistakes(I'm not English)->google translate
    Unfortunately, there is no standard method for unbuffered input.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    SO,I have to use getch()

    thanks anyway

    but equivalent to clrscr () , I do think not exists in header

    "system("cls");" is good?
    Last edited by nutzu2010; 12-29-2010 at 12:01 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    No, you should tell us which operating system and compiler you're using, so you can use the most appropriate "this works like getch() in conio.h"

    Using it isn't a problem, so long as you know it isn't portable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    windows xp2,codeblocks with mingw

    Using it isn't a problem, so long as you know it isn't portable.
    you mean the getch () or system ("cls")?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by nutzu2010 View Post
    windows xp2,codeblocks with mingw



    you mean the getch () or system ("cls")?
    Both. Personally, I would wrap any non-standard function calls in another function, for clarity (and maintainability).

    [edit]
    Example:

    Code:
    #ifdef WIN32
    #include <conio.h>
    #endif
    
    int unbuffered_input( void )
    {
    #ifdef WIN32
    	return getch( );
    #else
    #error "fatal: 'unbuffered_input' not yet implemented for this system"
    #endif
    }
    
    void clear_screen( void )
    {
    #ifdef WIN32
    	system( "cls" );
    #else
    #error "fatal: 'clear_screen' not yet implemented for this system"
    #endif
    }
    [/edit]
    Last edited by Sebastiani; 12-29-2010 at 12:53 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    There is a win32 "getch" replacement in the FAQ.

    There's a tutorial for win32 consoles here -> adrianxw.dk Win32 Tutorials Home
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    For clearing a screen, try printing a form feed character, like so:
    Code:
    putchar('\f');
    I know this works on Linux, but I'm not sure about Windows. It can't be any less portable than system("cls").

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    @Sebastiani good code ,but should be included "stdlib.h"
    i found here an exemplu about "win32" and I understand.

    @Salem tansks for FAQ ,but I do not understand how to install PDcurses

    @anduril462 your exemple does not work(probably ,is only for linux)


    I think I'll use the solution of Sebastiani or " system("cls") and getch() "
    Last edited by nutzu2010; 12-30-2010 at 03:03 AM.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by nutzu2010 View Post
    I think I'll use the solution of Sebastiani or " system("cls") and getch() "
    If you're going to use Sebastiani's code with the #else portion, you might as well put something other than ""fatal: 'clear_screen' not yet implemented for this system". You can use the putchar I gave you or use system("clear") to support Linux and the like:
    Code:
    void clear_screen( void )
    {
    #ifdef WIN32
    	system( "cls" );
    #else
    	system( "clear" );
    #endif
    }

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    Wonderful code,@anduril462!!!
    Thank you all for your answers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replacing character is character is different
    By s10062971 in forum C# Programming
    Replies: 4
    Last Post: 11-07-2010, 06:45 AM
  2. Replacing switch statement with a nested if/else
    By dev123 in forum C Programming
    Replies: 27
    Last Post: 09-06-2010, 12:30 AM
  3. Replacing Characters
    By carrotcake1029 in forum C Programming
    Replies: 3
    Last Post: 04-28-2008, 01:08 PM
  4. replacing a line in a file
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 05-19-2005, 10:38 AM
  5. Replacing a notebook drive
    By RoD in forum Tech Board
    Replies: 2
    Last Post: 03-29-2005, 07:07 AM