C++中cout輸出中文信息亂碼問(wèn)題及解決
cout輸出中文信息亂碼問(wèn)題
問(wèn)題描述
在實(shí)例化學(xué)生類(lèi)對(duì)象時(shí),對(duì)學(xué)生的姓名采用了形如“張三”這樣的漢字信息,在輸出學(xué)生姓名時(shí)出現(xiàn)了亂碼問(wèn)題(如下圖):
解決辦法
采用<windows.h>頭文件中的SetConsoleOutputCP(CP_UTF8)函數(shù)來(lái)設(shè)置在顯示器打印時(shí)的編碼格式就解決了亂碼問(wèn)題。
完整代碼如下:
#include <iostream> #include <windows.h> using namespace std; class Student { public: string name; int num; Student(const string &name, int num) : name(name), num(num) {} friend ostream &operator<<(ostream &os, const Student &student) { os << "name: " << student.name << " num: " << student.num; return os; } }; int main() { SetConsoleOutputCP(CP_UTF8); Student s("張三", 1001); cout << s << endl; return 0; }
C++ 輸出cout
#include <cstring> #include <iostream> using namespace std; int main() { /* <<運(yùn)算符 (可以進(jìn)行 混合 拼接輸出) */ const char *s = "hhhhh"; //字符串是const char*類(lèi)型的,所以將字符串賦值給 char* 類(lèi)型要加const關(guān)鍵字 cout << "the length of the s is " << strlen(s) << endl; // strlen()是cstring庫(kù)中的函數(shù) char str[10] = "ddddd"; cout << "hello" << endl; cout << s << str << endl; //可以進(jìn)行拼接輸出 // 如何打印字符串地址的值? //對(duì)于其他類(lèi)型的指針,c++將其對(duì)應(yīng)于void*,并打印地址的數(shù)值表示。如果要獲得字符串的地址,則必須將其強(qiáng)制類(lèi)型轉(zhuǎn)換成其他類(lèi)型 cout << &str[0] << endl; //invalid cout << (float *)s << endl; //valid cout << (void *)str << endl; //valid cout << (int *)"hello" << endl; //valid cout << "-------------------------------------------\n"; /* put()方法 (可以進(jìn)行 混合 拼接輸出) 用于顯示字符 (也可以將int型的賦給它,它會(huì)將int轉(zhuǎn)換成char,從而顯示與該ascll碼相對(duì)應(yīng)的字符) */ cout.put('d'); cout.put('\n'); cout.put('d').put('b').put('\n'); //可以進(jìn)行拼接輸出 cout.put(65); cout.put(65.9); //put 浮點(diǎn)數(shù)65.9強(qiáng)制類(lèi)型轉(zhuǎn)換成整型數(shù)65(向下取整) cout.put('\n'); cout << "-------------------------------------------\n"; /* write()方法 (可以進(jìn)行 混合 拼接輸出) 第一個(gè)參數(shù)提供了要顯示的字符串的地址,第二個(gè)參數(shù)指出要顯示多少個(gè)字符 */ const char *state1 = "Florida"; const char *state2 = "Kansas"; //state1、state3用于提供state2前面和后面的數(shù)據(jù),以便程序員知道程序錯(cuò)誤存取state2時(shí)發(fā)生的情況 const char *state3 = "Euphoria"; int len = strlen(state2); cout << "Increasing loop index:\n"; int i; for (i = 1; i <= len; i++) { cout.write(state2, i); cout << endl; } // concatenate output cout << "Decreasing loop index:\n"; for (i = len; i > 0; i--) cout.write(state2, i) << endl; // exceed string length cout << "Exceeding string length:\n"; //我們發(fā)現(xiàn):連續(xù)定義的字符串時(shí)連續(xù)存儲(chǔ)的,中間用一個(gè)空格隔開(kāi) ?。∵@可能因?yàn)榫幾g器之間的差別而有所不同 cout.write(state2, len + 5).write("\n", 1).write(state2, len + 4) << endl; /* write()也可以用于數(shù)值數(shù)據(jù),您可以將數(shù)字的地址強(qiáng)制轉(zhuǎn)換成char*,然后傳遞給他 但這不會(huì)將數(shù)字轉(zhuǎn)換為相應(yīng)的字符,而是傳輸內(nèi)存中儲(chǔ)存的位表示。例如4字節(jié)的long值將作為四個(gè)獨(dú)立的字節(jié)被傳輸, 輸出設(shè)備將把每個(gè)字節(jié)作為ASCLL碼進(jìn)行解釋?zhuān)罱K顯示出來(lái)的是四個(gè)字符的組合(有可能是亂碼) */ long val = 1094795585; // 二進(jìn)制數(shù)01000001010000010100000101000001所對(duì)應(yīng)的十進(jìn)制數(shù)(每個(gè)字節(jié)都是65) cout.write((char *)&val, sizeof(long)).write("\n", 1); cout << "-------------------------------------------\n"; /* 刷新輸出緩存區(qū) */ cout << "Hello, good-looking! " << flush; cout << "Wait just a moment, please." << endl; //endl 刷新緩沖區(qū),并插入一個(gè)換行符 flush(cout); cout << flush; //ostream類(lèi)對(duì)<<插入運(yùn)算符進(jìn)行了重載,使得下述表達(dá)式將被替換位函數(shù)調(diào)用flush(cout); return 0; }
輸出
the length of the s is 5
hello
hhhhhddddd
ddddd
0x406045
0x61feee
0x406063
-------------------------------------------
d
db
AA
-------------------------------------------
Increasing loop index:
K
Ka
Kan
Kans
Kansa
Kansas
Decreasing loop index:
Kansas
Kansa
Kans
Kan
Ka
K
Exceeding string length:
Kansas Euph
Kansas Eup
AAAA
-------------------------------------------
Hello, good-looking! Wait just a moment, please.
#include <iostream> using namespace std; int main() { int n = 10; cout << "n\n"; cout << n << " (decimal)\n"; cout << hex << n << " (hexadecimal)\n"; cout << oct << n << " (octal)\n"; dec(cout); // ostream類(lèi)重載了<<運(yùn)算符,這使得上述用法與函數(shù)調(diào)用dec(cout)等價(jià) cout << n << " (decimal)\n"; cout << "-------------------------------------------\n"; //width() 只影響接下來(lái)顯示的一個(gè)項(xiàng)目,然后字段寬度將恢復(fù)為默認(rèn)值 0 int w = cout.width(2); //width(int i)返回的是修改前字段寬度的值,而不是剛設(shè)置的值 //fill(char c) 它更改的填充字符將一直有效,直到再次更改它為止 cout.fill('*'); cout << "default field width = " << w << ":\n"; //C++的原則:顯示所有的數(shù)據(jù)比保持列的整潔更重要。輸入上面設(shè)置了字段寬度為2,但這里依舊能將字符串“default field width = ”顯示全 cout.width(5); cout << "N" << ":\n"; for (long i = 1; i <= 100; i *= 10) { cout.width(5); cout << i << ":\n"; } cout << "-------------------------------------------\n"; //設(shè)置浮點(diǎn)數(shù)的精度 float price1 = 20.40; float price2 = 1.9 + 8.0 / 9.0; cout << "\"Furry Friends\" is $" << price1 << "!\n"; cout << "\"Fiery Fiends\" is $" << price2 << "!\n"; cout.precision(2); //修改輸出浮點(diǎn)數(shù)的精度為2,設(shè)置后一直有效,直到再次更改它為止 cout << "\"Furry Friends\" is $" << price1 << "!\n"; cout << "\"Fiery Fiends\" is $" << price2 << "!\n"; cout.precision(6); cout.setf(ios_base::showpoint); //showpoint是ios_base類(lèi)聲明中定義的類(lèi)級(jí)靜態(tài)常量,在成員函數(shù)的定義外面使用要加上作用域運(yùn)算符(::) cout << "\"Furry Friends\" is $" << price1 << "!\n"; cout << "\"Fiery Fiends\" is $" << price2 << "!\n"; cout.precision(2); cout << "\"Furry Friends\" is $" << price1 << "!\n"; cout << "\"Fiery Fiends\" is $" << price2 << "!\n"; cout << "-------------------------------------------\n"; int temperature = 63; cout << "Today's water temperature: "; cout.setf(ios_base::showpos); // show plus sign cout << temperature << endl; cout << "For our programming friends, that's\n"; cout << std::hex << temperature << endl; // use hex cout.setf(ios_base::uppercase); // use uppercase in hex cout.setf(ios_base::showbase); // use 0X prefix for hex cout << "or\n"; cout << temperature << endl; cout << "How " << true << "! oops -- How "; cout.setf(ios_base::boolalpha); cout << true << "!\n"; cin.get(); return 0; }
輸出
n
10 (decimal)
a (hexadecimal)
12 (octal)
10 (decimal)
-------------------------------------------
default field width = 0:
****N:
****1:
***10:
**100:
-------------------------------------------
"Furry Friends" is $20.4!
"Fiery Fiends" is $2.78889!
"Furry Friends" is $20!
"Fiery Fiends" is $2.8!
"Furry Friends" is $20.4000!
"Fiery Fiends" is $2.78889!
"Furry Friends" is $20.!
"Fiery Fiends" is $2.8!
-------------------------------------------
Today's water temperature: +63
For our programming friends, that's
3f
or
0X3F
How 0X1! oops -- How true!
附錄
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)教務(wù)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)教務(wù)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03帶頭結(jié)點(diǎn)的鏈表的基本操作(超詳細(xì))
鏈表是一種動(dòng)態(tài)分配空間的存儲(chǔ)結(jié)構(gòu),能更有效地利用存儲(chǔ)空間,通過(guò)對(duì)單鏈表基本操作的代碼實(shí)現(xiàn),我深刻領(lǐng)悟到以“指針”指示元素的后繼,在插入或刪除元素時(shí)不需要移動(dòng)元素2023-07-07Visual Studio 2022中創(chuàng)建的C++項(xiàng)目無(wú)法使用萬(wàn)能頭<bits/stdc++.h>的
如果大家也遇到下面這種問(wèn)題,可能是沒(méi)有include文件夾中沒(méi)有bits/stdc++.h,這篇文章主要介紹了Visual Studio 2022中創(chuàng)建的C++項(xiàng)目無(wú)法使用萬(wàn)能頭<bits/stdc++.h>的解決方案,感興趣的朋友跟隨小編一起看看吧2024-02-02關(guān)于數(shù)據(jù)結(jié)構(gòu)單向鏈表的各種操作
這篇文章主要介紹了關(guān)于數(shù)據(jù)結(jié)構(gòu)單向鏈表的各種操作,關(guān)于數(shù)據(jù)結(jié)構(gòu)鏈表的操作一般涉及的就是增刪改查,下面將關(guān)于無(wú)空頭鏈表展開(kāi)介紹,需要的朋友可以參考下2023-04-04C語(yǔ)言 一級(jí)指針與二級(jí)指針詳細(xì)介紹
這篇文章主要介紹了C語(yǔ)言 一級(jí)指針與二級(jí)指針詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-10-10