V
V
vlarkanov2019-04-20 18:11:38
linux
vlarkanov, 2019-04-20 18:11:38

C++: Why does getch return -1 before I press any key?

Actually, subject. Here is the code and its output?

#include <stdio.h>
#include <iostream>
#include <curses.h>
#include <ncurses.h>
using namespace std;
int main(){
  int code;
  code = getch();
  std::cout << code;
}

-1

------------------
(program exited with code: 0)
Press return to continue

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ronald McDonald, 2019-04-20
@Zoominger

Pusho needs char , int for numbers only.

V
vanyamba-electronics, 2019-04-20
@vanyamba-electronics

do {
   code = getch();
} 
while(code == -1);
std::cout << code;

R
Roman, 2019-04-20
@myjcom

Mix https://en.cppreference.com/w/cpp/io
with https://en.cppreference.com/w/cpp/io/c
Not good. especially getch() is not a standard.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question