how to get getch function to work?

Discussion in 'Mac Programming' started by tombrownington, Jan 19, 2008.

  1. tombrownington macrumors newbie

    Joined:
    Jan 19, 2008
    #1
    hi,
    i am new to the mac, and just started using xcode. i recently learned that the conio.h - i used its getch function regularly - header file doesn't exist on unix platforms. searching for an alternative i found that the curses.h header file provides a similar function.
    i tried to write and compile this basic program in xcode but it gives me errors.

    #include <iostream>
    #include <curses.h>
    using namespace std;

    int main () {
    cout << "Hello, World!" <<endl;
    getch();
    return 0;
    }

    xcode gives me 2 building errors:
    1) "_stdscr", referenced from:
    _stdscr$non_lazy_ptr in main.o
    2) "_wgetch", referenced from:
    _main in main.o
    symbol(s) not found
    collect2: Id returned 1 exit status

    what should i do to get this program working (i'd rather compile directly from xcode rather than from console - i'm not too familiar with the latter). can anyone point me in the right direction?

    P.S. I'm running xcode 3 on an intel iMac with leopard installed.
     
  2. ScoobyMcDoo macrumors 65816

    Joined:
    Nov 26, 2007
    Location:
    Austin, TX
    #2
    You don't wanna use curses. Basically, you have to turn off line buffering to do a single character read. Here's an example:

    #include <iostream>
    #include <termios.h>

    int mygetch( );

    int main (int argc, char * const argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    char ch;
    ch = mygetch( );
    std::cout << "You hit the " << ch << " key" << std::endl;
    return 0;
    }

    int mygetch( ) {
    struct termios oldt,
    newt;
    int ch;
    tcgetattr( STDIN_FILENO, &oldt );
    newt = oldt;
    newt.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newt );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
    return ch;
    }
     
  3. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #3
    If you are running in Xcode, what need do you have of getch() anyway? You get a Run Log window that does not go bye-bye like the DOS box (ergo - command prompt window) does.

    Todd
     
  4. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  5. tombrownington thread starter macrumors newbie

    Joined:
    Jan 19, 2008
    #5
    I already did, and it works fine. What you suggest is probably the best alternative.
    Still out of curiosity I'd like to know why my original code doesn't compile. Anybody help me with that?
    Thanks for the help anyway, toddburch.
     
  6. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #6
    You original code, with the getch() turned into getchar(), and the curses.h include removed, works fine for me.

    Code:
    #include <iostream>
    using namespace std;
    
    int main () {
    cout << "Hello, World!" <<endl;
    getchar();
    return 0;
    }
    Todd
     
  7. fimac macrumors member

    Joined:
    Jan 18, 2006
    Location:
    Finland
    #7
    You need to add the curses library to the link command. (The error messages you get are simply the linker telling that it does not have enough information to finish linking.) The easiest way is to add "-lcurses" to your target's "Other Linker Flags".

    (Note: Curses requires initialization and shutdown calls, see "man curses" or search XCode help. You should not use printf/cout, instead curses provides printw et al.)

    Code:
    #include <iostream>
    #include <curses.h>
    using namespace std;
    
    int main(void)
    {
      initscr();
      cbreak();
      noecho();
      printw("Hello, world!");
      getch();
      endwin();
      return 0;
    }
    
     
  8. rdettogni macrumors newbie

    Joined:
    Nov 15, 2010
    #8
    Thanks!!

    I was having a similar problem and removing curses.h and using getchar solved everything!!!! Thanks!!!
     
  9. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #9
    curses.h SOUNDS like either the worst thing to include in your code, or the best depending on your intent.
     
  10. 7b7hdz macrumors newbie

    Joined:
    Jun 21, 2017
    #10
    Use the cin.get() function for example:

    #include <iostream>

    using namespace std;

    int main()
    {
    char input = cin.get();

    cout << "You Pressed: " << input;
    }

    //The program would then wait for you to press a key
    //Once you have, the key you pressed would be printed to the screen
     
  11. jaduff46 macrumors regular

    jaduff46

    Joined:
    Mar 3, 2010
    Location:
    Northern NJ
    #11
    Be aware that the previous post was 6 1/2 years old. Problem probably solved by now. LOL.
     

Share This Page