I want to read a string from the terminal and save it into a std::string as UTF-8.
When writing characters like áéíóú (which occupy 2 bytes) they are read as a single NUL character.
I tried this code in a Linux environment (without the SetConsoleCP things, obviously) and it works, so what do I need to do to make it work in Windows?
#include <iostream>
#include <windows.h>
void PrintStringByChar(const std::string& str) {
for (int i = 0; i < str.size(); i++) {
int x = str[i];
std::cout << "[" << x << "]" << std::endl;
}
}
int main() {
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
std::cout << "Write: ";
std::string text;
std::getline(std::cin, text);
PrintStringByChar(text);
}