c++输出字母错误
C++ wrong letter for output
你好,我有一个关于凯撒密码的新问题
关键:3
平原:ABCCDEFGĞ嗨İJKLMNOOPRSŞTUUVYZ
密码:DEFGĞHIİJKLMNOÖPRSŞTUÜVYZABCD
这是土耳其字母"cığ,o,ş,u, c,İ,Ğ,o,Ş,u"
我需要加密和解密和程序不应该有大小写敏感。应该是s= s, ç=Ç
你可以看到下面的程序,但是我有一些问题
1)文本(纯文本)和密钥应该由用户输入,但我不能这样做。
2) char text[] = "DEF";这个输入应该给出(用于解密)"CÇD"但是它给出了"CÃD"
通常它应该给出"Ç"而不是"Ã"
我需要帮助:(
# include <iostream>
# include <cstring>
const char alphabet[] ={'A', 'B', 'C', 'Ç', 'D', 'E', 'F', 'G', 'Ğ', 'H', 'I',
'İ', 'J', 'K', 'L', 'M', 'N', 'O', 'Ö', 'P', 'R', 'S',
'Ş', 'T', 'U', 'Ü', 'V', 'Y', 'Z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '.', ',', ':', ';', ' '};
const int char_num =44;
void cipher(char word[], int count, int key)
{
int i = 0;
while(i < count) {
int ind = -1;
while(alphabet[++ind] != word[i]) ;
ind += key;
if(ind >= char_num)
ind -= char_num;
word[i] = alphabet[ind];
++i;
}
}
void decipher(char word[], int count, int key)
{
int i = 0;
while(i < count) {
int ind = -1;
while(alphabet[++ind] != word[i]) ;
ind -= key;
if(ind < 0)
ind += char_num;
word[i] = alphabet[ind];
++i;
}
}
int main()
{
char text[] = "ABC";
int len = strlen(text);
std::cout << text << std::endl;
cipher(text, len, 2);
std::cout << text << std::endl;
decipher(text, len, 2);
std::cout << text << std::endl;
system("pause");
return 0;
}
这个问题是您的程序使用的编码与控制台期望的编码不同。Windows在默认情况下是这样配置的;程序使用cp1252或cp1254这样的编码,而控制台期望使用cp437这样的编码。
这是一篇来自微软开发人员的文章,解释了为什么会这样。
网上已经有很多关于解决编码不匹配的方法的信息
相关文章:
- 显示错误输出的简单数组排序程序
- Dijkstra 中的错误输出
- 自定义runtime_error,如果我在 #what 中使用#c_str(),则错误输出始终为空
- 流字符串错误输出
- C++ 异常处理错误输出
- C ++中的错误输出,i = 65530,当我们打印i的值时,它给出-6状态65530
- SSE和iostream:浮点类型的错误输出
- C++将 popen 错误输出读取为字符串
- 部分透视/高斯消除 - 交换列而不是产生错误输出的行
- C++,向量:获得包含 0 的错误输出
- 数组函数错误输出
- CC1plus 错误:输出文件名指定两次
- 字符串错误输出
- 子斯特的错误输出
- 对大量c++错误输出完全一无所知.我认为这与预定义的函数和迭代器有关
- 简单程序的错误输出
- XCode调试器lldb中断,没有错误输出,但程序运行良好
- std::string::append(std::string)错误输出
- 查看大错误输出的顶行
- 使用结构的错误输出