C++中cout輸出中文信息亂碼問題及解決
cout輸出中文信息亂碼問題
問題描述
在實例化學生類對象時,對學生的姓名采用了形如“張三”這樣的漢字信息,在輸出學生姓名時出現(xiàn)了亂碼問題(如下圖):
解決辦法
采用<windows.h>頭文件中的SetConsoleOutputCP(CP_UTF8)函數(shù)來設(shè)置在顯示器打印時的編碼格式就解決了亂碼問題。
完整代碼如下:
#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() { /* <<運算符 (可以進行 混合 拼接輸出) */ const char *s = "hhhhh"; //字符串是const char*類型的,所以將字符串賦值給 char* 類型要加const關(guān)鍵字 cout << "the length of the s is " << strlen(s) << endl; // strlen()是cstring庫中的函數(shù) char str[10] = "ddddd"; cout << "hello" << endl; cout << s << str << endl; //可以進行拼接輸出 // 如何打印字符串地址的值? //對于其他類型的指針,c++將其對應(yīng)于void*,并打印地址的數(shù)值表示。如果要獲得字符串的地址,則必須將其強制類型轉(zhuǎn)換成其他類型 cout << &str[0] << endl; //invalid cout << (float *)s << endl; //valid cout << (void *)str << endl; //valid cout << (int *)"hello" << endl; //valid cout << "-------------------------------------------\n"; /* put()方法 (可以進行 混合 拼接輸出) 用于顯示字符 (也可以將int型的賦給它,它會將int轉(zhuǎn)換成char,從而顯示與該ascll碼相對應(yīng)的字符) */ cout.put('d'); cout.put('\n'); cout.put('d').put('b').put('\n'); //可以進行拼接輸出 cout.put(65); cout.put(65.9); //put 浮點數(shù)65.9強制類型轉(zhuǎn)換成整型數(shù)65(向下取整) cout.put('\n'); cout << "-------------------------------------------\n"; /* write()方法 (可以進行 混合 拼接輸出) 第一個參數(shù)提供了要顯示的字符串的地址,第二個參數(shù)指出要顯示多少個字符 */ const char *state1 = "Florida"; const char *state2 = "Kansas"; //state1、state3用于提供state2前面和后面的數(shù)據(jù),以便程序員知道程序錯誤存取state2時發(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ù)定義的字符串時連續(xù)存儲的,中間用一個空格隔開 ?。∵@可能因為編譯器之間的差別而有所不同 cout.write(state2, len + 5).write("\n", 1).write(state2, len + 4) << endl; /* write()也可以用于數(shù)值數(shù)據(jù),您可以將數(shù)字的地址強制轉(zhuǎn)換成char*,然后傳遞給他 但這不會將數(shù)字轉(zhuǎn)換為相應(yīng)的字符,而是傳輸內(nèi)存中儲存的位表示。例如4字節(jié)的long值將作為四個獨立的字節(jié)被傳輸, 輸出設(shè)備將把每個字節(jié)作為ASCLL碼進行解釋,最終顯示出來的是四個字符的組合(有可能是亂碼) */ long val = 1094795585; // 二進制數(shù)01000001010000010100000101000001所對應(yīng)的十進制數(shù)(每個字節(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ū),并插入一個換行符 flush(cout); cout << flush; //ostream類對<<插入運算符進行了重載,使得下述表達式將被替換位函數(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類重載了<<運算符,這使得上述用法與函數(shù)調(diào)用dec(cout)等價 cout << n << " (decimal)\n"; cout << "-------------------------------------------\n"; //width() 只影響接下來顯示的一個項目,然后字段寬度將恢復為默認值 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è)置浮點數(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); //修改輸出浮點數(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類聲明中定義的類級靜態(tài)常量,在成員函數(shù)的定義外面使用要加上作用域運算符(::) 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!
附錄
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Visual Studio 2022中創(chuàng)建的C++項目無法使用萬能頭<bits/stdc++.h>的
如果大家也遇到下面這種問題,可能是沒有include文件夾中沒有bits/stdc++.h,這篇文章主要介紹了Visual Studio 2022中創(chuàng)建的C++項目無法使用萬能頭<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)于無空頭鏈表展開介紹,需要的朋友可以參考下2023-04-04