Electric Light Bulb symbol (Unicode) output to terminalby C++ - 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;
}

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.

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åäö马克

Incorrect output of wchar_t characters in console 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

How to create a folder on a Mac with C++?

How do you have the user input the folder name and have it created in the desktop (for mac)?
This is what I have so far.. (and extra code underneath)
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main ()
{
char game_name [100];
cout << "Game Name: ";
cin >> game_name;
const char* homeDir = getenv ("Home");
char final [256];
sprintf (final, "%s/Desktop/%s",homeDir, game_name);
mkdir(final,0775);
other code....
....
...
..
return 0;
}
Environment variables are case sensitive, so you need to use getenv("HOME") instead of getenv("Home").
Use Boost Library (though there will be overhead of setting up boost on your system but its worth for doing many other stuffs in C++): boost::filesystem::create_directories()
#include <boost/filesystem.hpp>
// your code....
boost::filesystem::create_directories("/bla/a");

How do you make a program sleep in C++ on Win 32?

How does one "pause" a program in C++ on Win 32, and what libraries must be included?
#include <windows.h>
Sleep(number of milliseconds);
Or if you want to pause your program while waiting for another program, use WaitForSingleObject.
In C++11, you can do this with standard library facilities:
#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));
If you are using boost, you can use the thread::sleep function:
#include <boost/thread/thread.hpp>
boost::system_time time = boost::get_system_time();
time += boost::posix_time::seconds(1);
boost::thread::sleep(time);
Otherwise, you are going to have to use the win32 api:
#include <windows.h>
Sleep(1000);
And, apparently, C++0x includes this:
#include <thread>
std::this_thread::sleep_for(chrono::seconds(1));
Please note that the code above was tested on Code::Blocks 12.11 and Visual Studio 2012
on Windows 7.
For forcing your programme stop or wait, you have several options :
sleep(unsigned int)
The value has to be a positive integer in millisecond.
That means that if you want your programme wait for 2 second, enter 2000.
Here's an example :
#include <iostream> //for using cout
#include <stdlib.h> //for using the function sleep
using namespace std; //for using cout
int main(void)
{
cout << "test" << endl;
sleep(5000); //make the programme waiting for 5 secondes
cout << "test" << endl;
sleep(2000); // wait for 2 secondes before closing
return 0;
}
If you wait too long, that probably means the parameter is in second. So change it like that :
sleep(5);
For those who get error message or problem using sleep try to replace it by _sleep or Sleep especially on Code::Bloks.
And if you still getting probleme, try to add of one this library on the biggining of the code.
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
system("PAUSE")
A simple "Hello world" programme on windows console application would probably close before you can see anything. That the case where you can use system("Pause").
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
system("PAUSE");
return 0;
}
If you get the message "error: 'system' was not declared in this scope" just add
the following line at the biggining of the code :
#include <cstdlib>
cin.ignore()
The same result can be reached by using cin.ignore() :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.ignore();
return 0;
}
cin.get()
example :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.get();
return 0;
}
getch()
Just don't forget to add the library conio.h :
#include <iostream>
#include <conio.h> //for using the function getch()
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
getch();
return 0;
}
You can have message telling you to use _getch() insted of getch
If you wish for the program to stay responsive while "paused", you need to use a timer event.
It depends on what type of program you are writing.
A console app can just call Sleep. A GUI app probably does not want to do this, as all the menus and widgets will go insensitive, and the app won't redraw itself during this period. Instead you need to do something like set yourself up a timer with a callback when it expires.
Dont use a sleep function in your GUI if it is not provided by the framework you are working with. This could create referencing problems to data (specially in a thread that is not the main thread). This could freeze you GUI. Its not just a question of sleeping for a short time , use waitmutexes if you need to do that.