I haven't seen before! getch() doesn't work!

This is a discussion on I haven't seen before! getch() doesn't work! within the C++ Programming forums, part of the General Programming Boards category; Hello, i compiled this programm under linux: $g++ -o test test.cpp -lncurses #include <stdio.h> #include <curses.h> int main(void) { char ...

  1. #1
    StefanA.
    Guest

    Arrow I haven't seen before! getch() doesn't work!

    Hello,

    i compiled this programm under linux: $g++ -o test test.cpp -lncurses

    #include <stdio.h>
    #include <curses.h>

    int main(void)
    {
    char c;

    // while ((c = getchar()) != '\n'); //emtpy input buffer
    c = getch();
    printf("%c\n", c);
    }

    The output:

    $ ./test
    d

    $

    Why does the programm recognize EVERY key as ""??

    pls help
    regards
    StefanA.

  2. #2
    stovellp
    Guest
    Code:
    int main(void)
    {
    char c;
    
    // while ((c = getch()) != '\n') //emtpy input buffer
         {
         printf("%c\n", c);
         }
    You had a ';' after your loop, dont you want to keep checking the letter and printing it if its not a '\n'? Try that.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    getch() is included in the Conio.h Library.

    it should be
    Code:
    #include <stdio.h>
    #include <curses.h>
    #include <conio.h>
    NOTE use codetags
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>getch() is included in the Conio.h Library.
    But (s)he said:
    >>i compiled this programm under linux: $g++ -o test test.cpp -lncurses

    getch() lives in ncurses libraries too.

    You didn't init the screen, which I expect is your problem.

    Try this:
    Code:
    #include <stdio.h>
    #include <curses.h>
    
    int main(void)
    {
      int c;
      
      initscr();
      noecho();
      for ( ;; )
      {
        c = getch();
        printw("%c\n", c);
      }
      endwin();
      
      return 0;
    }
    Use CTRL+C to terminate it.

    This is *nix talk, if you have any more questions, post them on the Linux forum and someone will help you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    StefanA.
    Guest
    Hello,

    thanks for your help!

    with other words, i have chosen the wrong function!
    Is there any getch() like function, which isn't declared in conio.h or curses.h? I want the input not to be displayed on screen.

    I ask, because i am programming under linux and cannot use conio.h and don't want to use the complex curses library...


    regards,
    StefanA.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Correct me if I am wrong, but isn't getch unique to DOS?

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by sundeeptuteja
    Correct me if I am wrong, but isn't getch unique to DOS?
    Correcting.... Getch() is in the conio.h header and isn't limited to
    DOS (might it be in dos at all!).

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by sundeeptuteja
    Correct me if I am wrong, but isn't getch unique to DOS?
    You're wrong Didn't you gather that from my previous post!?!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    I guess so. However, the help file in Turbo C++ v3.0 said that getch (and getche) are unique to DOS. Howcome?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. program wont work, why?
    By chasingxsuns in forum C Programming
    Replies: 11
    Last Post: 02-02-2006, 04:38 PM
  5. getch() acting up...
    By Govtcheez in forum C Programming
    Replies: 9
    Last Post: 08-18-2001, 01:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21