Thread: getchar() and variant

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    32

    getchar() and variant

    Hello,
    I need code this below but I need something like a getchar() that doens't wait for the "return" from the user. How can I implement it? (it only wait for a key pressed)
    Code:
    	int ch;
    	while ( (ch = getchar() ) != '\n') {
    	
    		cout << "ch " << ch << endl;
    	}

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to use system specific functions. There is no standard way to do it, but there is a FAQ on the subject "How do I get user input without waiting for Enter" or some such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    32
    I should curious to implement it by myself...

  4. #4
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    depending on the platform try kbhit().

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For Dev-C++, try getche() in <conio.h>. Read the FAQ matsp indicated: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed