Reputation: 23
My program runs fine in the IDE (Visual Studio 2022), in debug and release modes.
When I make a build and want to start the .exe
from Explorer, it starts and runs, but... well, have a look:
This is how it should be:
This is what it looks like outside of VS:
So far, I have tried to set the Runtime Library to Multi-threaded (/MT), but that didn't work.
Otherwise, I really don't seem to find much. It seems the standalone .exe
is missing some dependencies, but I can't figure out what I need to do. From my understanding, everything I have included in the header should get compiled "into" the .exe
as well.
The int128_t
doesn't seem to work. Neither do the ANSI color codes.
The timer is working, though.
The code:
#include <iostream>
#include <chrono>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
bool isPrime(mp::int128_t n);
mp::int128_t n{ 0 }, y{ 0 };
int main()
{
std::cin >> y;
std::cin >> n;
std::cout << "\n";
for (y; y <= n; y++)
{
int lengthy = to_string(y).length();
const auto start = std::chrono::steady_clock::now();
if (isPrime(y) == true)
std::cout << "\033[1;7;32m" << std::setw(lengthy) << std::left << y << "\033[0m ";
else
{
std::cout << std::setw(lengthy) << std::left << y << " ";
}
const auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed = end - start;
if (elapsed.count() >= 0.1)
std::cout << "\033[1;7;36m" << std::setw(10) << std::left << elapsed.count() << "\033[0m ";
else
{
std::cout << "\033[1;36m" << std::setw(10) << std::left << elapsed.count() << "\033[0m ";
}
}
std::cout << "\n";
std::cin >> y;
}
bool isPrime(mp::int128_t n)
{
if (n == 2 || n == 3)
return true;
if (n <= 1 or n % 2 == 0 or n % 3 == 0)
return false;
for (uint64_t i = 5; i * i <= n; i += 6)
{
if (n % i == 0 or n % (i + 2) == 0)
return false;
}
return true;
}
Upvotes: 1
Views: 623
Reputation: 1
Windows terminal supports ANSI escape sequences but not in default. You have to set up in some cases. This is the solution:
Windows Start --> run regedit --> add new variable to HKEY_CURRENT_USER/Console (name: VirtualTerminalLevel type: double word value type: decimal value: 1)
Upvotes: 0
Reputation: 1150
I've been able to enable Color Codes in default Windows terminal using the following code:
#ifdef _WIN32
#include <Windows.h>
void enableColors()
{
DWORD consoleMode;
HANDLE outputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
if (GetConsoleMode(outputHandle, &consoleMode))
{
SetConsoleMode(outputHandle, consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}
}
#endif
Call it once at the start of the main
function.
Upvotes: 5
Reputation: 11311
See https://en.wikipedia.org/wiki/ANSI_escape_code, specifically:
In 2016, Microsoft released the Windows 10 version 1511 update which unexpectedly implemented support for ANSI escape sequences, over two decades after the debut of Windows NT.[13] This was done alongside Windows Subsystem for Linux, allowing Unix-like terminal-based software to use the sequences in Windows Console. Unfortunately this defaults to off, but Windows PowerShell 5.1 enabled it. PowerShell 6 made it possible to embed the necessary ESC character into a string with `e.[14] Windows Terminal, introduced in 2019, supports the sequences by default, and Microsoft intends to replace the Windows Console with Windows Terminal.[15]
Color codes work in Visual Studio Code (Terminal).
ADDITIONAL INFO:
https://devblogs.microsoft.com/commandline/new-experimental-console-features/
Upvotes: 4