C++ not printing emojis as expected - c++

I have the following extract of a program
int main(){
cout << "1) ✊\n";
cout << "2) ✋\n";
cout << "3) ✌️\n";
}
But at the time I run it I get strange texts like the following
====================
rock paper scissors!
====================
1) 
2) 
3) ԣÅ
This seems not to be related to my terminal but instead to a compilation result because if I run echo ✊ it shows as expected.
See example below
I'm currently using the following compilation commands and compiler version
g++ *.cpp -o rock_paper_scissors.exe
g++.exe (Rev9, Built by MSYS2 project) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
Finally, note that it was working before as expected, but at some point, it stopped working, I noticed after I used system("pause") which I'm guessing may have changed something on the compilation configurations as this is a Windows-only command, I delete such piece of code and still having the issue.
You can see the rest of the code here: https://github.com/guillene/RockPaperScissors

If your terminal font supports emojis and you don't want to write much code (like switching from cout to wcout), you can use the windows api function below.
#include <windows.h>
#include <iostream>
int main(){
SetConsoleOutputCP(CP_UTF8);
std::cout << "1) ✊\n";
std::cout << "2) ✋\n";
std::cout << "3) ✌️\n";
return 0;
}
see: https://learn.microsoft.com/en-us/windows/console/setconsoleoutputcp

Related

Correct settings for C++ to recognize compiler options?

In VS Code I have a define identifier that is not found. The variable is eventually passed to the compiler as a flag -DENABLE_LOGS_PREVIEW and found in ./cmake-build-debug/build.ninja. I build with CMake and Ninja directly from the command line.
#ifdef ENABLE_LOGS_PREVIEW
std::cout << "Hello world" << std::endl;
#endif
std::cout << "Goodbye world" << std::endl;
Correctly prints, though VS Code greys out std::cout << "Hello world" << std::endl;
Hello world
Goodbye world
I'm struggling to get the correct c_cpp_properties.json to work with my workflow. How do I get the above define to be recognized?
The answer here was to have CMake to generate a compile_commands.json and add it to the VS Code c_cpp_properties.json like so:
"compileCommands": "${workspaceFolder}/cmake-build-debug/compile_commands.json"

G++ Compiles, Executable Won't Run

I've been using a text editor and g++ to compile and run programs for a while, but for some reason, it stopped working today.
It will compile without errors but when I try to run the executable, it doesn't do anything. No errors, no output. Nothing. This is the code I'm trying to run.
#include <iostream>
#include <fstream>
int main(){
// Initializes variable.
int coordinatePair;
// Creates an object to use for the file.
std::ofstream fileReader;
// Initiates a for-loop to get the value of each variable.
for(int i = 0; i == 5; i++){
std::cout << "Please enter the x-coordinate of your coordinate pair. " << std::endl << "You have " << 5 - i << " pairs left to enter." << std::endl;
std::cin >> coordinatePair;
// Opens the file.
fileReader.open("points.txt");
// Writes the user's values to the file.
fileReader << coordinatePair << std::endl;
// Closes the file.
fileReader.close();
}
}
In the terminal, I created a directory to the file location...
cd ~/file location
And then I compiled it.
g++ points_out.cpp -o points_out
And I tried to run it.
./points_out
And nothing happens. No error messages, no output, no nothing. While the code isn't exactly efficient (I'm probably better off opening and closing the file outside of the for-loop.) it should still run. I tried putting in incorrect code to see what would happen, and it gave me the proper error codes. I also tried
g++ -W -Werror points_out.cpp
...and that didn't give me any error codes either. I tried making a new directory to a different .cpp file I had compiled and ran earlier this week and it worked just fine. For some reason, this one just will not run. And I'm confident that it is compiling because the executable is being created. I'm also confident that it is not running, because the text file is not being created.
I got this thing to compile successfully and run once and it created the text file when it did. This is the first time I've run into issues with g++. I've looked everywhere for a solution but most of the people who had this problem just weren't doing the
./points_out
part or had issues with MingW or needed to uninstall Avast. None of these are the issue I have so I'm at a loss.
Also, not sure if this will help, but running
g++ --version
gets me the following output:
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The behavior is normal for a program doing nothing and completing successfully.
The C++ for-loop evaluates the loop condition before entering the loop body. When the loop body is completed, the optional post-loop expression is evaluated and the program returns to the conditional check.
So your code assigns the value 0 to i and then tests to see if i == 5 before allowing entry into the loop.
It's not entirely clear what you were intending, but you probably mean't
for (int i = 0; i < 5; i++)
We can demonstrate that quickly with this piece of code:
#include <iostream>
int main() {
std::cout << "original loop:\n";
for (int i = 0; i == 5; i++)
std::cout << i << '\n';
std::cout << "corrected loop:\n";
for (int i = 0; i < 5; i++)
std::cout << i << '\n';
}
Live demo: http://ideone.com/O0Ul2z
-- Addendum --
Most programmers would have thought to add something like a no-frills, no-conditions
std::cout << "hello world\n";
at the top of their program to verify that it wasn't running vs not working.

Enabling `-std=c++14` flag in Code::Blocks

I have installed Code::Blocks for Windows and want to compile C++14 code like generic lambdas but the binary version of Code::Blocks that I've installed from codeblocks.org doesn't support the flag -std=c++14.
How do I update the compiler and enable -std=c++14 flag for Code::Blocks?
To compile your source code using C++14 in Code::Blocks, you, first of all, need to download and install a compiler that supports C++14 features.
Here’s how you can do it on Windows:
Download MinGW from here (particular build) or from official site to choose options
Extract it to for example: C:\ (result will be C:\MinGW)
Open Code::Blocks
Go to Settings => Compiler.
Go to “Toolchain Executables”.
In the top field “Compiler’s installation directory”, change the directory to the one where you extracted the compiler. E.g C:\MinGW.
Change all the necessary files under “Program Files” to match the files under C:\MinGW\bin:
Before you hit “OK”, go to the leftmost tab “Compiler settings”.
Select “Compiler Flags”.
For simplicity, right-click in the list somewhere and select “New Flag”:
Type in the following and click "OK", and tick the box of the flag you just created:
Lastly, you need to specify the debugger path. Go to "Settings" => "Debugger", click "Default" on the left-hand side and enter the new full path of the executable:
Now, try to compile a program with C++14 features:
#include <iostream>
#include <string>
using namespace std;
auto main() -> int
{
auto add_two([](auto x, auto y){ return x + y; });
cout << add_two("I"s, "t"s) << " works!" << endl;
}
May a humble newbie make one small suggestion? A small modification to test C++14 code, to allow resulting .exe file to be run independently of the IDE it was created in, slightly modified test program follows:
#include <iostream>
#include <string>
using namespace std;
auto main() -> int
{
auto add_two([](auto x, auto y){ return x + y; });
cout << add_two("I"s, "t"s) << " works!" << endl;
cout << "press enter to continue..." << endl;
cin.ignore(10, '\n');
cin.get();
}
Thank you all, peace to all fellow coders, especially Igor Tandetnik.

Can't get an output from my c++ application

I'm using Microsoft Visual C++ Express, and I'm wondering what's wrong with the following application, I can't get an output, it gives me an error. Since I'm not English, the error is in a different language, but it basically says something along the lines of 'The system can't find the given/stated path'.
I have literally checked it a dozen times but I can't seem to find what I'm doing wrong. Here's the code:
#include <iostream>
using namespace std;
int DemoConsoleOutput ()
{
cout << "This is a simple string literal" << endl;
cout << "Writing number five:" << 5 << endl;
cout << "Performing division 10/5 = " << 10 / 5 << endl;
cout << "Pi when approximated is 22 / 7 = " << 22 / 7 << endl;
cout << "Pi more accurately is 22 / 7 = " << 22.0 / 7 << endl;
return 0;
}
int main()
{
return DemoConsoleOutput ();
}
Can anyone help me find what's wrong with this?
Addendum: I also keep getting a weird error on my computer every couple minutes. It says: 'a program can't display a message on the desktop' and it gives me the options to show the message, or to give me another reminder in a few minutes. When I click 'show message' my screen goes black for a second and it then gives an error 'The application data folder for Microsoft visual c++ 2010 express could not be created'.
I don't know if it's important, but my OS is windows vista.
From the situation it seems that the application has insufficient rights to create a working folder and write an file in that folder. Possible causes can be that your anti virus is not letting VS do so or the folder has no write rights. Please go through this question.

Netbeans 7.0 compiler error

I am using Netbeans 7.0 and get this error when I try to compile, and debug:
make: *** [.validate-impl] Error 127
BUILD FAILED (exit value 2, total time: 281ms)
I set my environment variables (within Windows) to C:\cygwin\bin
Within Netbeans my build tools are of the Cygwin family. The C compiler is Gcc, C++ compiler is G++, Assembler is as.exe, make command is make.exe, and debugger is gdb.exe. They're all located within C:\cygwin\bin\FILENAMEHERE
And finally, my source code:
#include <iostream>
int main ()
{
std::cout << "Enter two numbers:" << std::endl;
int v1, v2;
std::cin >> v1 >> v2;
std::cout << "The sum of " << v1 "and " << v2 << "is" << v1 +v2 << std::endl;
return 0;
}
Any suggestions?
I've had a LOT of problems with the 7.0 C++ version. It keeps destroying the auto generated make files. I removed it and downgraded. The 6.9.1 version is still available and seems to work much better.
p.s.
If you're going to be doing QT development it matters which compiler chain you choose. You want to look for "mingw tdm dw2". The SJLJ version of mingw (which is the default) does not work with the released QT binary libraries.