广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++中cout输出中文信息乱码问题及解决
  • 410
分享到

C++中cout输出中文信息乱码问题及解决

cout输出中文乱码C++乱码C++cout输出中文乱码 2022-11-13 19:11:07 410人浏览 安东尼
摘要

目录cout输出中文信息乱码问题问题描述解决办法c++ 输出cout输出输出附录cout输出中文信息乱码问题 问题描述 在实例化学生类对象时,对学生的姓名采用了形如“张三

cout输出中文信息乱码问题

问题描述

在实例化学生类对象时,对学生的姓名采用了形如“张三”这样的汉字信息,在输出学生姓名时出现了乱码问题(如下图):

解决办法

采用<windows.h>头文件中的SetConsoleOutputcp(CP_UTF8)函数来设置在显示器打印时的编码格式就解决了乱码问题。

完整代码如下:

#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关键字
    cout << "the length of the s is " << strlen(s) << endl; // strlen()是cstring库中的函数
    char str[10] = "DDDdd";
    cout << "hello" << endl;
    cout << s << str << endl; //可以进行拼接输出
    // 如何打印字符串地址的值?
    //对于其他类型的指针,c++将其对应于void*,并打印地址的数值表示。如果要获得字符串的地址,则必须将其强制类型转换成其他类型
    cout << &str[0] << endl;        //invalid
    cout << (float *)s << endl;     //valid
    cout << (void *)str << endl;    //valid
    cout << (int *)"hello" << endl; //valid
    cout << "-------------------------------------------\n";
    
    cout.put('d');
    cout.put('\n');
    cout.put('d').put('b').put('\n'); //可以进行拼接输出
    cout.put(65);
    cout.put(65.9); //put 浮点数65.9强制类型转换成整型数65(向下取整)
    cout.put('\n');
    cout << "-------------------------------------------\n";

    
    const char *state1 = "Florida";
    const char *state2 = "Kansas";
    //state1、state3用于提供state2前面和后面的数据,以便程序员知道程序错误存取state2时发生的情况
    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";
    //我们发现:连续定义的字符串时连续存储的,中间用一个空格隔开 !!这可能因为编译器之间的差别而有所不同
    cout.write(state2, len + 5).write("\n", 1).write(state2, len + 4) << endl;

    
    long val = 1094795585; // 二进制数01000001010000010100000101000001所对应的十进制数(每个字节都是65)
    cout.write((char *)&val, sizeof(long)).write("\n", 1);
    cout << "-------------------------------------------\n";

    
    cout << "Hello, Good-looking! " << flush;
    cout << "Wait just a moment, please." << endl; //endl 刷新缓冲区,并插入一个换行符
    flush(cout);
    cout << flush; //ostream类对<<插入运算符进行了重载,使得下述表达式将被替换位函数调用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类重载了<<运算符,这使得上述用法与函数调用dec(cout)等价
    cout << n << " (decimal)\n";
    cout << "-------------------------------------------\n";

    //width()  只影响接下来显示的一个项目,然后字段宽度将恢复为默认值 0
    int w = cout.width(2); //width(int i)返回的是修改前字段宽度的值,而不是刚设置的值
    //fill(char c) 它更改的填充字符将一直有效,直到再次更改它为止
    cout.fill('*');
    cout << "default field width = " << w << ":\n"; //C++的原则:显示所有的数据比保持列的整洁更重要。输入上面设置了字段宽度为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";

    //设置浮点数的精度
    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); //修改输出浮点数的精度为2,设置后一直有效,直到再次更改它为止
    cout << "\"Furry Friends\" is $" << price1 << "!\n";
    cout << "\"Fiery Fiends\" is $" << price2 << "!\n";

    cout.precision(6);
    cout.setf(ios_base::showpoint); //showpoint是ios_base类声明中定义的类级静态常量,在成员函数的定义外面使用要加上作用域运算符(::)
    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!

附录

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: C++中cout输出中文信息乱码问题及解决

本文链接: https://www.lsjlt.com/news/171052.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作