如何获取特殊字符输入并存储在字符或字符串变量中?特殊字符表示©或 ž

how to get spcecial charecter input and store in char or string variable? special characters means © or ž

本文关键字:变量 字符串 字符 表示 特殊字符 存储 获取 何获取 字符输入      更新时间:2023-10-16
// Microsoft visual studio 2010
#include <iostream>
#include <string>
using namespace std;
int main()
{
    char password;
    string strPassword;
    cin >> password; // only for single character 
    cin >> strPassword // for string storage
    /* 1. but when I give input © it will store as single character c, not as ©.
    I want to store it as © in my variable 'password' or 'strPassword' so  that I can compare it with my pre-stored password
    2. I also want to know about stdint.h 
   what is it ? why we need it and how it helps us (especially for streaming).
    */
    return 0;
}

关于文本编码的第一个问题,您需要程序假定与通过输入接收到的文本编码相同。在Windows中这样做的一种方法是检查您的ANSI代码页是什么,例如通过命令wmic os get codeset。例如,美国人或挪威人将获得1252,Windows ANSI Western代码页。

然后配置你的控制台窗口,例如包括

system( "chcp 1252" );

在你的程序开始。

一般来说,您需要使用Unicode来处理更大的字符集。请参阅我的Q+A “我如何使Unicode iostream I/o在Windows和Unix-land中都工作?”希望它没有因为编译器的进化而失效。


关于stdint.h的第二个问题,我建议你谷歌一下。这肯定是SO上的复制品。SO不是一个教程网站