1

I have seen many posts about this, but none of them answer the question, they give examples that do not work, all you get is more error messages, or just sent off at other tangents. ncurses is continually mentioned yet none of the examples I have found work on OS X, despite the claims. Either the examples are wrong or they are not actually tested before posting. I would add a comment to these posts, but as I am a new user I am not allowed to ask anything about them, which is also ridiculous as that would be far easier than having to start a new topic.

I want the program to ask a question, wait for user input and read each key pressed without the return key being pressed, I was some years ago fairly proficient in Turbo Pascal, and this was so easy to do like most things in Pascal, it would just work... I thought C++ would be similar, instead you are just continually faced with contrary platform specific use cases, and examples that never compile.

I am using CLion 2017.2.2 on OS X.

  • Possible duplicate of [Capture characters from standard input without waiting for enter to be pressed](https://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed) – Thomas Sablik Oct 19 '18 at 12:47
  • As far as I know there is no platform independent solution. You can use ncurses for mac and linux and conio for windows. – Thomas Sablik Oct 19 '18 at 12:50

1 Answers1

0

Here is an example code for ncurses. I tested it under linux but it should also work under mac os.

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


int main(void) {
    WINDOW * mainwin;

    if ( (mainwin = initscr()) == NULL ) {
          fprintf(stderr, "Error initialising ncurses.\n");
          exit(EXIT_FAILURE);
    }

    mvaddstr(13, 33, "Input: ");
    refresh();
    char input[2];
    input[0] = getch();
    input[1] = '\0';
    mvaddstr(15, 33, "Your Input is: ");
    mvaddstr(15, 48, input);
    mvaddstr(17, 33, "Press any key to exit");
    getch();

    delwin(mainwin);
    endwin();
    refresh();

    return EXIT_SUCCESS;
}

It's necessary to link against ncurses. I use cmake to manage my build:

cmake_minimum_required(VERSION 3.5)
project(ncurses)
set(CMAKE_CXX_STANDARD 11)

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra")

find_package(Curses REQUIRED)

add_executable(ncurses main.cpp)
target_link_libraries(ncurses ${CURSES_LIBRARIES})
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • seems some of the problems were with the CMakeLists.txt, using yours solved the initial errors with the above code. But now I get: "Error opening terminal: unknown. Process finished with exit code 1", displayed in the built in CLion terminal, this is a problem I've had before, yet the Terminal in CLion settings is set to the default OS X Terminal as far as I can see, so not sure what the issue is here. – CpƆlCuƆsCpƆlCuƆsƆ Oct 21 '18 at 00:42
  • NCurses does not work with the CLion terminal. You have to start your program with your system terminal – Thomas Sablik Oct 21 '18 at 10:10
  • Ahh OK, think I remember that now, this whole experience seems to be a bit makeshift.. works fine now, thanks. 90's software actually seemed a lot better – CpƆlCuƆsCpƆlCuƆsƆ Oct 23 '18 at 10:40
  • Though how am I suppose to debug if I can't use ncurses in the built in clion console/terminal? Is there a way of debugging externally to terminal? – CpƆlCuƆsCpƆlCuƆsƆ Oct 25 '18 at 11:51
  • Clion uses gdb. You can use gdb in terminal or connect Clion to your process – Thomas Sablik Oct 26 '18 at 09:30