-1

Im currently using wchar_t for a username, however, I'm having an issue when the username contains a space when using std::wcout as it will just get the first word typed. I am aware of getline for when your user input contains a space but I can't get it to work with the wchar_t. I don't suppose anyone could help or point me in the right direction?

Here is the code I have currently:

                

wchar_t window_title[50] = L"Krogex: ";                
  std::cout << "Input window " << i + 1 << "s username: " << std::endl; 
    wchar_t username[20]; 
    std::wcin >> username;
user438383
  • 5,716
  • 8
  • 28
  • 43
Kryton
  • 95
  • 1
  • 5
  • 5
    What happened when you tried to use `getline`? Is there a reason why you're using an array instead of `std:: wstring`? – ChrisMM Jan 16 '23 at 11:05

1 Answers1

2

Here is how to use getline with a wstring; this will allow you to enter a username with spaces.

wchar_t window_title[50] = L"Krogex: ";
std::cout << "Input window " << i + 1 << "s username: " << std::endl;
std::wstring username;
std::getline(std::wcin, username);
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
user20716902
  • 861
  • 1
  • 14