如何在C++中打印多行(非标准)unicode文本字符串?为Clarity更新!(希望如此)



用更多关于我要求的知识重写这个问题;(感谢James Risner和Turtle的帮助,但我的措辞不正确,得到了与所需不同的回复(


我目前正在为我的类编写一个程序,在这个程序中,我在字符串中打印出非标准的Unicode字符。这些字符是网站上的直接副本,不是u\####标准副本,而是预先选择的unicode字符。我正在运行的程序是Clion,使用mingw的忍者构建设置构建我的程序

我遇到的问题是,我的输出,而不是unicode字符,而是(我认为(不相关字符的随机数组。在Clion的"调试"菜单中打印它会输出正确的输出,但在运行时或其自己的外部文件中打印它都会输出问题。以下是我的代码的精确副本(逐字符(请不要重复使用:(

#include <iostream>
#include <string>
#include <windows.h>
#define _WIN32_WINNT 0x0500
#include <thread>
#include <chrono>
#include <random>
using namespace std;
static int Range(int start, int end){
random_device rd;
mt19937 rng(rd());
uniform_int_distribution<int> dist(start,end);
return dist(rng);
}
int main() {
system("color 0F");
HWND consoleWindow = GetConsoleWindow();
int windowSize = 390;
MoveWindow(consoleWindow, windowSize,windowSize,windowSize,windowSize, TRUE); // This program and the one below it not only locks the window size, but also locks the window at a fixed display pixel length/width
SetWindowLong(consoleWindow, GWL_STYLE, GetWindowLong(consoleWindow, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX);
ShowScrollBar(GetConsoleWindow(), SB_VERT, 0);
string name = "33[91m▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀n33[92m███╗░░░███╗░█████╗░██████╗░███████╗███╗░░██╗██╗n33[93m████╗░████║██╔══██╗██╔══██╗██╔════╝████╗░██║██║n33[94m██╔████╔██║██║░░██║██║░░██║█████╗░░██╔██╗██║██║n33[95m██║╚██╔╝██║██║░░██║██║░░██║██╔══╝░░██║╚████║██║n33[96m██║░╚═╝░██║╚█████╔╝██████╔╝███████╗██║░╚███║██║n33[91m╚═╝░░░░░╚═╝░╚════╝░╚═════╝░╚══════╝╚═╝░░╚══╝╚═╝n33[92m▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀n";
float balance = Range(1,50000);
float withD, cSelect;
int x = 0;
while (name[x] != '') {
cout << name[x] << flush;
this_thread::sleep_for(chrono::milliseconds(1));
x++;
}
Sleep(2);
std::cout << 'n' << endl;
string bottomBar = "33[93m░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░n";
string systName = "33[94mModeni Systems LLC";
cout << "              ";
cout << systName << "n";
std::cout << 'n' << endl;

int y = 0;
while (bottomBar[y] != '') {
cout << name[y] << flush;
this_thread::sleep_for(chrono::milliseconds(1));
y++;
}
string Input;
cout << "n           33[92mPlease Input your Name...nn              ";
getline(cin, Input);
cout << endl;
cout << "          Welcome " << Input << "!" << endl;
cout << " Please select a number from the options below!n" << endl;
cout << bottomBar << endl;
cout << "         1.)  View your Balancen         2.)  Make a withdrawaln         3.)  Depositn         4.)  Customer Supportn         5.)  Log Out Securelyn" << endl;
cout << bottomBar << endl;
cin >> cSelect;
while(cSelect != 5){
if(cSelect==1){
cout << "Your current balance is... " << balance << " dollars!n n" <<  endl;
cout << "Please select a number from the options given below!n" << endl;
cout << bottomBar << endl;
cout << "        1.)View your Balancen        2.)Make a withdrawaln        3.)Depositn        4.)Customer Supportn        5.)Log Out Securely" << endl;
cin >> cSelect;
} else if(cSelect==2){
cout << "Please enter the amount you'd like to withdraw!n" << endl;
cin >> withD;
if(withD>balance){
cout << "Sorry, you do not have that much money! Please try again... " << endl;
} else if(withD<=balance) {
balance = balance - withD;
cout << "Successfully taken out " << withD << " dollars!n" << "Your new balance is " << balance << " dollars!" << endl;
cout << "Please select a number from the options given below!n" << endl;
cout << bottomBar << endl;
cout << "        1.)View your Balancen        2.)Make a withdrawaln        3.)Depositn        4.)Customer Supportn        5.)Log Out Securely" << endl;
cin >> cSelect;
}
} else if(cSelect==3){
cout << "Please enter the amount you'd like to deposit!" << endl;
double depAm;
cin >> depAm;
balance = balance + depAm;
cout << "Your new balance is now " << balance << " dollars!" << endl;
cout << "Please select a number from the options given below!n" << endl;
cout << bottomBar << endl;
cout << "1.)View your Balancen2.)Make a withdrawaln3.)Depositn4.)Customer Supportn5.)Log Out Securely" << endl;
cin >> cSelect;
} else if(cSelect==4){
cout << bottomBar << "n" << endl;
cout << "Hello! This is Modeni's Self-Service Assistant!n     Please describe your problem below! n" << endl;
string proB;
cin >> proB;
int chatF;
chatF = Range(0,5);
if(chatF==0){
cout << "We're so sorry to hear that! Please wait as we get you in touch with someone who can help.n" << endl;
} else if(chatF==1){
cout << "Sorry to hear that you're currently having that problem! Please sit tight as we get you in touch with someone who can help.n" << endl;
} else if(chatF==2){
cout << "That's not good! Please wait just a moment as we get you in touch with someone who can help.n" << endl;
} else {
cout << "Ouch! Just give us a moment while we put you in touch with someone who can help!n" << endl;
}
Sleep(10000);
string nameSt;
int nameVar;
nameVar = Range(0,5);
if(nameVar == 1)
nameSt = "Raphael";
else if(nameVar == 2)
nameSt = "Marie";
else if(nameVar == 3)
nameSt = "Joesph";
else
nameSt = "Marian";
cout << "    33[94m░█░█ " << nameSt << " has joined the chat █░█░" << endl;
Sleep(Range(3000,8000));
cout <<"n >> Just a moment while I look over your concern.n" << endl;
Sleep(Range(3000,8000));
cout << " >> Alright. I'm sorry you're dealing with this problem right now. Let's put you in touch with one of our call-in agents to assist you further.n" << endl;
Sleep(Range(3000,8000));
cout << " >> Their number is - 1-(918)-335-1300.n" << endl;
Sleep(Range(3000,8000));
cout << " >> Is there anything else I can help you with today? n" << endl;
return 0;
}
}
return 0;
}

我专门为windows编写这个程序,如果不重写整个代码,我还没有找到一个可靠的解决方案。预期输出是照片1,而实际输出是照片2。感谢您的帮助!

照片1

照片2

您的程序没有问题。您的显示器出现问题。

% cc -o modeni modeni.cpp -lc++
% ./modeni
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
███╗░░░███╗░█████╗░██████╗░███████╗███╗░░██╗██╗
████╗░████║██╔══██╗██╔══██╗██╔════╝████╗░██║██║
██╔████╔██║██║░░██║██║░░██║█████╗░░██╔██╗██║██║
██║╚██╔╝██║██║░░██║██║░░██║██╔══╝░░██║╚████║██║
██║░╚═╝░██║╚█████╔╝██████╔╝███████╗██║░╚███║██║
╚═╝░░░░░╚═╝░╚════╝░╚═════╝░╚══════╝╚═╝░░╚══╝╚═╝
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
% echo $TERM
xterm-256color

我使用的是macOS系统,这在iTerm2和终端上都适用TERM=xterm-256color。

什么是TERM

这个环境变量建议应用程序需要什么终端模拟才能在屏幕上正确显示字符。应用程序将使用termcap/terminfo数据库来查找正确的转义序列,以显示颜色、在屏幕上移动或操纵文本以及其他效果。

我现在只见过xterm-256彩色。为什么

现代终端应用程序假定输出为xterm-256彩色格式。许多人不再可以选择其他格式。

AFAIK C++中没有标准的方法来处理标准io中的unicode,如果您只将unicode字符串设置为std::string进行输出,不同操作系统的默认控制台将表现不同;例如,在Windows cmd中,可能需要_setmode(_fileno(stdout), _O_U16TEXT);才能正确显示UTF-16字符串。

好消息是,在C++23中,将有<print>(希望,但目前还不确定(来帮助解决这个麻烦的问题。

最新更新