Search code examples
c++consolewchar-t

How do you print a whole wchar_t with spaces at once? (c++)


I am trying to build a command line application, i'm at the start, and I am trying to get an wchar_t input and print it, but if I type in "foo foof" for example, it prints foo>>> foof>>>. Here is my code:

#include <iostream>
using namespace std;
int main()
{
    while (1 == 1)
    {
        wchar_t afterprint[100];
        wcout << "\n>>> ";
        wcin >> afterprint;
        wcout << afterprint;

    }
    return 0;
}

And this is what happens in the console:

>>> foo foof fofof
foo
>>> foof
>>> fofof
>>>

What I am expecting to happen, is for it to print what was entered, on one line.

Help would be highly appreciated, and I am sorry if the answer is really obvious, because I am new to C++.


Solution

  • I see the question is evolved from getting 1 char at a time problem to getting 1 word at a time problem. You can use fgetws to capture the whole input:

    while (1)
    {
        wchar_t afterprint[100];
        std::wcout << "\n>>> ";
        fgetws(afterprint, 100, stdin);
        std::wcout << afterprint;
    }