0

Trying to get user input for a simple terminal game. I am on Mac OS.

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

int main()
{
    int ch;
    while (ch != 113)
    {
        ch = getch();
        std::cout << ch << std::endl;
    }

    return 0;
}

In this example, I'm trying to simply print my keystrokes, but ch = getch() doesn't seem to do anything. It doesn't wait for a keypress and std::cout << ch << std::endl just prints -1 repeatedly. Can't figure out what I'm doing wrong here.

ptan9o
  • 174
  • 9

1 Answers1

3

You need to first call initscr before any other curses functions. http://www.cs.ukzn.ac.za/~hughm/os/notes/ncurses.html

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

int main()
{
    int ch;
    initscr(); // <----------- this
    while (ch != 113)
    {
        ch = getch();
        std::cout << ch << std::endl;
    }

    return 0;
}
algrebe
  • 1,621
  • 11
  • 17
  • Thank you, it's responding to keystrokes now. I had a feeling it was something small like that. – ptan9o May 28 '20 at 03:40