Incorrect output of wchar_t characters in console c++ - c++

I try to print wchar_t characters by their integer index to console, but some of them displays like empty rectangles, while with some of them (Chinese characters for example) it works fine.
I have folowing code:
#include <iostream>
#include <iomanip>
#include <fcntl.h>
#include <io.h>
#include <conio.h>
#include <codecvt>
#include <cmath>
using namespace std;
int main() {
_setmode(_fileno(stdout), _O_U16TEXT);
wcout << L"Example 1: " << wchar_t(25500) << "\n";
wcout << L"Example 2: " << wchar_t(831) << "\n";
return 0;
};
And this is how it looks in console
I think it's something to do with console font (I'm using MS Gothic), but I don't know what

Related

Converting Turkish-I letter to lowercase using boost in CPP

Since a few days I was trying to get a C++ code that converts the Turkish I character to lowercase ı correctly on VS2022 on Windows.
As I understand, Turkish I has the same Unicode as regular Latin I, thus, I need to define the locale as Turkish before converting, I used the following code:
#include <clocale>
#include <cwctype>
#include <fstream>
#include <iostream>
#include <locale>
#include <string>
int main() {
std::wstring input_str = L"I";
std::setlocale(LC_ALL, "tr_TR.UTF-8"); // This should impact std::towlower
std::locale loc("tr_TR.UTF-8");
std::wofstream output_file("lowercase_turkish.txt");
output_file.imbue(loc);
for (wchar_t& c : input_str) {
c = std::towlower(c);
}
output_file << input_str << std::endl;
output_file.close();
}
It worked fine on Linux, outputing ı, but didn't work correctly on Windows and it outputed i inplace of ı.
After some research I think it is a bug in Windows unicode/ascii mapping, so I went to an alternative solution, using an external library called boost, here is my code:
#include <boost/algorithm/string.hpp>
#include <string>
#include <locale>
#include <iostream>
#include <fstream>
using namespace std;
using namespace boost::algorithm;
int main()
{
std::string s = "I";
std::locale::global(std::locale{ "Turkish" });
to_lower(s);
ofstream outfile("output.txt");
outfile << s << endl;
outfile.close();
return 0;
}
again, outputing i inplace of ı. also using to_lower_copy outputs the same.

Electric Light Bulb symbol (Unicode) output to terminalby C++

I'm trying to output the symbol of Electric Light Bulb with code U+1F4A1 to Windows Terminal (experiment with Unicode). I can't realize how to do that. I tried to use wchar_t, wcout, to change console output code page, and with no result. Who made it. please tell how to do that.
#include <uchar.h>
#include <iostream>
#include <cstdlib>
#include <clocale>
#include "Windows.h"
#include <io.h>
#include <fcntl.h>
int main() {
SetConsoleCP(12000);
SetConsoleOutputCP(12000);
/*Alternative*/
system("chcp 65001");
std::cout << u8"\u1F4A1" << std::endl;
return 0;
}

std::wcout printing unicode characters but they are hidden

So, the following code:
#include <iostream>
#include <string>
#include <io.h>
#include <fcntl.h>
#include <codecvt>
int main()
{
setlocale(LC_ALL, "");
std::wstring a;
std::wcout << L"Type a string: " << std::endl;
std::getline(std::wcin, a);
std::wcout << a << std::endl;
getchar();
}
When I type "åäö" I get some weird output. The terminal's cursor is indented, but there is no text behind it. If I use my right arrow key to move the cursor forward the "åäö" reveal themselves as I click the right arrow key.
If I include English letters so that the input is "helloåäö" the output is "hello" but as I click my right arrow key "helloåäö" appears letter by letter.
Why does this happen and more importantly how can I fix it?
Edit: I compile with Visual Studio's compiler on Windows. When I tried this exact code in repl.it (they use clang) it works like a charm. Is the problem caused by my code, Windows or Visual Studio?
Windows requires some OS-specific calls to set up the console for Unicode:
#include <iostream>
#include <string>
#include <io.h>
#include <fcntl.h>
// From fctrl.h:
// #define _O_U16TEXT 0x20000 // file mode is UTF16 no BOM (translated)
// #define _O_WTEXT 0x10000 // file mode is UTF16 (translated)
int main()
{
_setmode(_fileno(stdout), _O_WTEXT); // or _O_U16TEXT, either work
_setmode(_fileno(stdin), _O_WTEXT);
std::wstring a;
std::wcout << L"Type a string: ";
std::getline(std::wcin, a);
std::wcout << a << std::endl;
getwchar();
}
Output:
Type a string: helloåäö马克
helloåäö马克

"endl" causes "C1001" error

My code is a basic HelloWorld but fails to compile when I use cout<<endl.
I'm using Microsoft visual studio fresh download and created a console application for my first test project.
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
It generates a
"C1001" at line 1.
Replacing "endl" with ""\n"" works though.
You don't need the precompiled header #include <stdafx.h> so you can safely get rid of it. Also get rid of using namespace std; because it pollutes the global namespace. Try something like this. There's no reason it shouldn't work.
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
In Visual Studio you can disable use of the precompiled header in the project settings.
I do not see what the problem is. Both options compile and execute for me.
RexTester cppOnline
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
cout << "lets see: " << s << "\n";
return 0;
}
So idk what was causing the error but it was fixed after pasting imports to the "stdafx.h" header file and then delete them...

Print content of a web page issue

I've no idea what's wrong with my code, but it does print nothing to stdout, although there is some content as shown in a debugger.
#include "stdafx.h"
#include <afx.h>
#include <afxinet.h>
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include "wininet.h"
using namespace std;
void DisplayPage(LPCTSTR pszURL)
{
CInternetSession session(_T("Mozilla/5.0"));
CStdioFile* pFile = NULL;
pFile = session.OpenURL(pszURL);
CString str = _T("");
while ( pFile->ReadString(str) )
{
wcout << str.GetString() << endl; // <-- here I expect some output, get nothing
// not even newline !
}
delete pFile;
session.Close();
}
// --- MAIN ---
int _tmain(int argc, _TCHAR* argv[])
{
DisplayPage( _T("http://www.google.com") );
cout << "done !" << endl;
cin.get();
return 0;
}
It is a console project. Console window pops up with message "done !" displayed only.
If anybody interested the issue was caused by non-OEM characters recieved from a web page trying to write to the default console (expecting OEM chars, translating mode). At the first non-OEM character std::wcout stops processing.
Either set the console to binary mode or convert recieved string to the appropriate encoding before sending to standard output.
#include <fcntl.h>
#include <io.h>
...
int old_transmode = _setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << str.GetString() << std::endl; // print wide string characters
...
_set_mode(_fileno(stdout), old_transmode); // restore original console output mode