0

I am currently building Tetris on the command line, and I'm working with getch() to recieve input without a deliminating character and without pausing the code to take input. However, when I enable the function with initscr(), all my output is lost.

#include <ncurses.h>
#include <iostream>

using std::cout;
using std::endl;

//created variables, set up, etc etc

int main(){

  initscr();
  cbreak();
  keypad(stdscr, TRUE);
  noecho();

  while (true){
    cout << "hello" << endl; //complex things are printed here
    int input;
    while (true){
      input = getch();
      if (input == -1){
        break;
      } else {
        return 1; //my initial goal was just to get getch() to work, so the input reponse is to end the program
      }
    }
    cout << "goodbye" << endl; //other complex things are printed here
  }
  return 0;
}

Without initscr() turned on, all my output printed (but getch() didn't work). With it, I get a blank screen. Why is this happening, and how can I fix it?

  • From the `getch` man page: *The getch, wgetch, mvgetch and mvwgetch, routines read a character from the window. In no-delay mode, if no input is waiting, the value ERR is returned. **In delay mode, the program waits until the system passes text through to the program.** Depending on the setting of cbreak, this is after one character (cbreak mode), or after the first newline (nocbreak mode). In half-delay mode, the program waits until a character is typed or the specified timeout has been reached.* So... Which mode have you selected? – user4581301 Jun 09 '23 at 00:25
  • @user4581301 Good catch: I hadn't specified, so it seems to have been automatically set to delay mode. – Heensle Jun 09 '23 at 00:47
  • @user4581301 My problem, however, has now shifted to altered output instead of missing output. With nodelay mode on, each "hello" and "goodbye" are printed on the next line down, over however many characters were in the previous word. I don't know stackoverflow protocol: should I open a new question for this? – Heensle Jun 09 '23 at 00:50
  • @Heensle You should open a new question. – john Jun 09 '23 at 04:47
  • This answers your question: [Can you use printf() and ncurses functions at the same time in C?](https://stackoverflow.com/questions/60551647/can-you-use-printf-and-ncurses-functions-at-the-same-time-in-c) (more than one duplicate) – Thomas Dickey Jun 10 '23 at 14:12

0 Answers0