output read string between getchar() calls

Mar 8, 2023 at 2:10pm
Hello mates,

i dont understand what my code is doning and why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    std::string msg;
    int c;

    while ((c = getchar()) != '\n')
    {
        msg.push_back(c);
        std::cout << "\nentered so far: " << msg << "\n";
    }
}



i dont understand why the output of f.ex. "Hello" is:
1
2
3
4
5
6
Hello
entered so far: H
entered so far: He
entered so far: Hel
entered so far: Hell
entered so far: Hello


i expect this behaviour:
1
2
3
4
5
6
7
8
9
10
H
entered so far: H
e
entered so far: He
l
entered so far: Hel
l
entered so far: Hell
o
entered so far: Hello


Why the hell is the actual variable msg not pushed back after each char entry but in the end?
Does anyone know how to achieve this?
Thank you very much.

sincerely,
Luke
Last edited on Mar 8, 2023 at 2:11pm
Mar 8, 2023 at 2:13pm
Input through command line is line buffered. That means the input you type will not be received by the program until you press Enter.
Last edited on Mar 8, 2023 at 2:16pm
Mar 8, 2023 at 2:15pm
thank you, this sounds plausible. is there any way to achieve my behaviour?
Last edited on Mar 8, 2023 at 2:23pm
Mar 8, 2023 at 2:21pm
Not using only standard C++. You can do it with the help of curses libraries (e.g. pdcurses on Windows and ncurses elsewhere). System dependent APIs, such as the "WinAPI" on Windows, can also do it (this is probably how the curses libraries are implemented).
Last edited on Mar 8, 2023 at 2:21pm
Mar 8, 2023 at 2:23pm
Thank you. ill have a look at that.
i also heared about getch() but i dont understand the documentation. if i replace getchar() with getch() from conio.h, i dont get any key printed to console but do get feedback from std::cout...
if i could just see the keystrokes..
Mar 8, 2023 at 2:29pm
It was a very long time since I used conio.h and I don't remember what you could do with it exactly.

The curses libraries also have their own version of getch(). By calling other functions you can configure how you want it to behave. Whether you want the the letters to be printed, whether you want to wait for input or return immediately, whether you want to read "key codes" rather than characters, etc.

I don't remember all the details but I think the curses libraries have more features compared to conio.h. Other things you can do is change the color and position of the text.
Last edited on Mar 8, 2023 at 2:35pm
Mar 8, 2023 at 2:44pm
Among the curses libraries I only really have experience with ncurses, but it looks mostly the same as pdcurses so I suspect it should be possible to write code that works with both.
Last edited on Mar 8, 2023 at 2:46pm
Mar 8, 2023 at 2:44pm
Thank you very much. ive just figured out how getch works
1
2
3
c = getch()
putchar(c)
...


does the thing with even no additional include lib.
Last edited on Mar 8, 2023 at 2:45pm
Mar 8, 2023 at 4:54pm
if i replace getchar() with getch() from conio.h, i dont get any key printed to console


Correct. getch() doesn't echo the typed char. If you want the typed char to be echoed then use getche().

Note that for Windows VS, these should be _getch() and _getche().

The other functionality provided with conio.h that may be of interest is kbhit() (or _kbhit() ). This returns non-zero if a character has been entered and 0 if not- without obtaining the char.
Topic archived. No new replies allowed.