在char上使用toupper返回char的ascii号,而不是字符? [英] Using toupper on char returns the ascii number of the char, not the character?

查看:70
本文介绍了在char上使用toupper返回char的ascii号,而不是字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main()
{

char hmm[1000];

cin.getline(hmm, 1000);
cout << hmm << endl;    //this was to test if I could assign my input to the array properly
for (int sayac = 0; hmm[sayac] != '@'; sayac++) {
    if (!isdigit(hmm[sayac])) {
        if (islower(hmm[sayac])) 
            cout << toupper(hmm[sayac]);
        else if (isupper(hmm[sayac]))
            cout << tolower(hmm[sayac]);
        else
            cout << hmm[sayac];
    }
}

编写一个程序,该程序将键盘输入读取到@符号并回显输入除数字外,将每个大写字符转换为小写,反之亦然.(不要忘记cctype家族.)"

"Write a program that reads keyboard input to the @ symbol and that echoes the input except for digits, converting each uppercase character to lowercase, and vice versa. (Don’t forget the cctype family.) "

我正在从入门书中进行此练习.但是,当我运行它时,它返回char的ascii顺序,而不是字符的大写/小写版本.无法解决问题.有人可以告诉我为什么吗?

I'm doing this exercise from the primer book. But when I run it, it returns the ascii order of the char, not the uppercase/lowercase version of the character. Couldn't figure out the problem. Can someone tell my why please?

(我可能对该练习有其他问题,如果有,请不要更正.我想自己修复它(我解释的问题除外),但是我无法检查其他问题有这个问题.

(I may have other problems about the exercise, please don't correct them if I have. I want to fix it on my own (except the problem I explained), but I can't check the other ones as I have this problem.

推荐答案

编写时

std::cout << toupper('a');

发生以下情况:

  1. int toupper(int ch) 会被调用,并返回一个整数,其值为'A'( 0x41 ).
  2. std :: basic_ostream :: operator<<(std::cout,0x41) 被调用,这是 int (2)重载,因为提供了 int .
  1. int toupper(int ch) is called, and returns an integer whose value is 'A' (0x41).
  2. std::basic_ostream::operator<<(std::cout, 0x41) is called, that is the int (2) overload since an int was provided.

总体上,它显示"65".

Overall, it prints "65".

作为解决方案,您可以将大写字母回退为 char :

As a solution, you can cast back your upper case to a char:

std::cout << static_cast<char>(toupper('a'));

这篇关于在char上使用toupper返回char的ascii号,而不是字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆