2

The following code:

#include <iostream> using std::wcin; using std::wcout; using std::locale; int main() { locale::global(locale("Portuguese_Brazil")); wcout << "wcin Test using \"ção\": "; // shows that wcout works properly wchar_t wcinTest[] = L""; wcin >> wcinTest; wcout << wcinTest << " should be \"ção\"."; return 0; } 

Results in:

wcin Test using "ção": ção ╬Æo should be "ção". 

The ╬ character is U+2021 or 8225, and the ç is U+00E7 or 231.

I changed mult-bytes option, set and not set UNICODE in project properties. Nothing worked.

I already set the console font into Consolas, a true type font capable of displaying the ç character correctly.

I'd like this as simple and reproducible possible to use as a standard practice for future UNICODE console applications.

Any ideas?

2 Answers 2

4

wcinTest is a wchar_t buffer of length 1;

You overflow it when you read into it. Use a std::wstring insead.

Sign up to request clarification or add additional context in comments.

2 Comments

Still, no good. Same result with wstring. This is what I tried: ' #include <iostream>' #include <string> using std::wcin; using std::wcout; using std::wstring; using std::locale; //using namespace std; int main() { locale::global(locale("Portuguese_Brazil")); wcout << "wcin Test using \"ção\": "; wstring wcinTest; wcin >> wcinTest; wcout << wcinTest << " should be \"ção\"."; return 0; }`
I think I'm on to something. I just realized that I broke my cin while fixing my cout output with locale("Portuguese_Brazil"). I removed that and use #include <Windows.h> and everything works!
2

This finally worked:

#include <iostream> #include <string> #include <Windows.h> using std::cin; using std::cout; using std::string; int main() { SetConsoleOutputCP(1252); SetConsoleCP(1252); cout << "wcin Test using \"ção\": "; // shows that wcout works properly string wcinTest; cin >> wcinTest; cout << wcinTest << " should be \"ção\"."; return 0; } 

I'm too newbie to understand why I need both SetConsoleOutputCP and SetConsoleCP. I though maybe just SetConsoleCP would fix everything, but no, I need both: SetConsoleOutputCP fixed cout; and SetConsoleCP fixed cin.

Thanks anyway @StoryTeller

1 Comment

I agree, but it solved my specific issue, even if I had to drop the utf8 for that. Current C++ handles this much better and the console just needs to use unicode font.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.