C++ toupper () 不起作用

C++ toupper () doesn't work

本文关键字:不起作用 toupper C++      更新时间:2023-10-16

我不知道为什么它在我的程序中不起作用。tolower()正常工作。现在我不知道toupper()是如何工作的,我认为它起作用为tolower()

#include <iostream>
#include <cctype>
int main ()
{
    using namespace std;
    char ch;

    while (ch != '@')
    {
        cin >> ch;
        if (isdigit (ch))
            cout << "";
        else if (isgraph(ch) )
        {
            ch = tolower (ch);
            cout << ch;
        }
        else
        {
            ch = toupper (ch);
            cout << toupper (ch);
        }
    }
    return 0;
}

函数std::isgraph如果字符具有图形符号,则返回true。然后,所有可见字符再次将true作为来自此功能的输出,因此所有人都会击中tolower。如果将所有迹象显示为std::tolower

在此处检查std::isgraph

如果您在程序中输入角色,天气是一个较低的案例或上限字母,它将始终在第一个其他陈述中结束:

else if (isgraph(ch) )
{
    ch = tolower (ch);
    cout << ch;
}

因此,您必须首先检查输入是否在上/下情况下。例如,Isupper和Islower应该有帮助。

#include <iostream>
#include <cctype>
int main ()
{
    using namespace std;
    char ch;
    while (ch != '@') {
        cin >> ch;
        if (isdigit (ch)) {
            cout << "this was a digit" << endl;
        }
        else if (isgraph(ch) && isupper(ch)) {
            ch = tolower (ch);
            cout << ch << endl;
        }
        else if (isgraph(ch) && islower(ch))
        {
            ch = toupper (ch);
            cout << ch << endl;
        }
    }
    return 0;
}