[go: nahoru, domu]

 
how to read a single character as with getchar but in C++?

Jul 4, 2024 at 8:35pm UTC
Hi,

in C one could read a single character from the keyboard with functions like getc() or getchar(). What is the equivalent in C++? streams use buffered input so how to do it?


This code does not force the user to exit input mode after 1 key is pressed:

1
2
char x;
cin >> x;


Then you also need to discard the rest of the characters entered...

I know it is trivial, but how is it done?

Jul 4, 2024 at 11:52pm UTC
JUANDENT wrote:
in C one could read a single character from the keyboard with functions like getc() or getchar(). What is the equivalent in C++? streams use buffered input so how to do it?


https://en.cppreference.com/w/cpp/io/basic_istream/get

std::get() ?

The first 4 constructors don't seem to use a buffer.

maybe std::read ?
https://en.cppreference.com/w/cpp/io/basic_istream/read
Jul 5, 2024 at 7:43am UTC
read a single character from the keyboard


Do you want the function to return immediately a char is entered or have to wait until a line termination char is entered? .read()/.get() etc will wait until a line termination char has been entered before they return (even if 1 char is requested).

If you want the function to return immediately a char is entered then I'm not aware of this in standard c++. If you have conio.h include file (windows) then there's _getch()/_getche().
Last edited on Jul 5, 2024 at 7:44am UTC
Jul 5, 2024 at 2:51pm UTC
Reportedly the curses library has its own getch function available.

https://linux.die.net/man/3/getch

While the curses library is for Linux there are ports for Windows. Two readily available for use via vcpkg. ncurses and pdcurses.

I can not say if either of those has getch available or how to set up the terminal. I have neither the time nor interest to learn yet another 3rd party library when standard C++20/23 are at the top of my 'to learn' queue.
Jul 5, 2024 at 3:13pm UTC
As with many 3rd party libraries ncurses functionality is not as easy to use as <conio.h>, it requires a bit of nudging to initialize and proper shutdown to work.

https://stackoverflow.com/a/75704765/9718560

Gee, there are Windows libraries which require a similar initialization/shutdown procedure to work. DirectX and the Common Controls libraries are two examples.
Jul 5, 2024 at 4:26pm UTC
you would think this would be simple or easy, but it isn't. I tried to do this for a while, and without inline assembler, I could not do it. C++ is not set up to read the hardware directly, which is what you need... you need to tap the keyboard interrupts as they happen, at a lower level than c++ gives you access to.
Jul 5, 2024 at 5:19pm UTC
You have to use the OS/terminal facilities to turn off line-buffered input (to “raw” input) AND to obtain the key press.

Meaning, there is no easy cross-platform way to do it. I assume you are still messing with Windows?

Check out the Windows Console functions (https://learn.microsoft.com/en-us/windows/console/console-functions). In particular, SetConsoleMode() and ReadConsoleInput().
Last edited on Jul 5, 2024 at 5:19pm UTC
Jul 8, 2024 at 3:14pm UTC
I don't understand the specific, negative comparison with the C library. The C functions have the same limitations as their C++ equivalents. Both are buffered.

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>

int main(void)
{
    int ch = getc(stdin); // input: Aaaaa
    printf("%d\n", ch);     // output: 65 (A)
    
    int ch2 = getc(stdin);
    printf("%d\n", ch2);    // output: 97 (a)
}


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
    char ch;
    cin >> ch; // input: Aaaaa
    cout << (int)ch << '\n'; // output: 65 (A)
    
    char ch2;
    cin >> ch2;
    cout << (int)ch2 << '\n'; // output: 97 (a)
}


Last edited on Jul 9, 2024 at 1:46pm UTC
Jul 8, 2024 at 3:47pm UTC
in C one could read a single character from the keyboard with functions like getc() or getchar()


How? As Ganado states above, these c functions are also line buffered. Yes, they will return a single character but only after a line terminator has been entered. Is this what is meant by 'reading a single character'?

What os and c compiler are you using? Can you post the c code you're using for this.
Last edited on Jul 8, 2024 at 3:52pm UTC
Registered users can post here. Sign in or register to post.