Thomas Boxley Thomas Boxley - 1 month ago 12x
C++ Question

How do I input variables using cin without creating a new line?

Whenever I input a variable using

, after one hits enter it automatically goes to a new line. I'm curious if there's a way to use
without having it go to a new line. I'm wanting to
multiple things on the same line in the command prompt. Is this possible?


You can't use cin or any other standard input for this. But it is certainly possible to get the effect you are going for. I see you're on Windows using Visual Studio, so you can use, for example, _getch. Here's an example that reads until the next whitespace and stores the result in a string.

#include <conio.h> // for _getch

std::string get_word()
    std::string word;
    char c = _getch();
    while (!std::isspace(c))
        std::cout << c;
        c = _getch();
    std::cout << c;
    return word;

It's not very good. For example, it doesn't handle non printing character input very well. But it should give you an idea of what you need to do. You might also be interested in the Windows API keyboard functions.

If you want a wider audience, you will want to look into some cross-platform libraries, like SFML or SDL.