islower()函数在C++中不起作用。我应该怎么办?

huangapple go评论63阅读模式
英文:

islower() function not working in C++. What should I do?

问题

这是代码的翻译部分:

#include <iostream>
using namespace std;
int main()
{
    char c;
    cin >> c;
    if (islower(c) == true)
        cout << "Lower";
    else
        cout << "Upper";
    return 0;
}

这是 islower() 函数的测试代码输出:

c
Upper

对于 isupper()

#include <iostream>
using namespace std;
int main()
{
    char c;
    cin >> c;
    if (isupper(c) == true)
        cout << "Upper";
    else
        cout << "Lower";
    return 0;
}

这是 isupper() 函数的测试代码输出:

C
Upper
英文:

I was writing a code and could not get my mistake. Finally, when I did, I was surprised.
islower() is not working or maybe I am stressed out of doing coding for the past 2 hours. Will anyone help me out here? But the function isupper() is working fine. I am using VS Code.

This is the code I wrote to test the islower() function:

#include &lt;iostream&gt;
using namespace std;
int main()
{
    char c;
    cin&gt;&gt;c;
    if(islower(c)==true)
    cout&lt;&lt;&quot;Lower&quot;;
    else
    cout&lt;&lt;&quot;Upper&quot;;
    return 0;
}

Here is the output:

c
Upper

For isupper():

#include &lt;iostream&gt;
using namespace std;
int main()
{
    char c;
    cin&gt;&gt;c;
    if(isupper(c)==true)
    cout&lt;&lt;&quot;Upper&quot;;
    else
    cout&lt;&lt;&quot;Lower&quot;;
    return 0;
}

Here is the output:

C
Upper

答案1

得分: 4

查看这些函数的确切定义。isupper()islower() 返回 int,而你正在将一个 int 与一个 bool 常量进行比较。根据整数提升规则,当 boolint 比较时,它会变成一个值为1(true)或0(false)的 int

将以下代码添加到你的程序以查看发生了什么:

    cout << "low " << islower(c) << endl;
    cout << "up " << isupper(c) << endl;

你会发现,当这些函数返回“真值”时,它们返回的值不是1。

当按照以下方式编写你的程序时,你的程序将正常工作:

#include <iostream>
using namespace std;
int main()
{
  char c;
  cin >> c;
  if (isupper(c)) {
    cout << "Upper";
  } else {
    cout << "Lower";
  }
  return 0;
}

这是因为 if 将任何非零值视为“真值”。你可能会在描述这个概念的上下文中找到关于“真值”值和“真值性”的引用。

英文:

Look at the precise definitions of these functions. isupper() and islower() return int, and you are comparing an int with a bool constant. The bool when compared to an int, by integer promotion rules becomes an int with the value 1 (true) or 0 (false).

Add this to your program to see what is happening:

    cout &lt;&lt; &quot;low &quot; &lt;&lt; islower(c) &lt;&lt; endl;
    cout &lt;&lt; &quot;up &quot; &lt;&lt; isupper(c) &lt;&lt; endl;

You will find that these functions when returning "truth" are returning a value that is not 1.

Your program will work properly when written along the lines of:

#include &lt;iostream&gt;
using namespace std;
int main()
{
  char c;
  cin&gt;&gt;c;
  if (isupper(c)) {
    cout&lt;&lt;&quot;Upper&quot;;
  } else {
    cout&lt;&lt;&quot;Lower&quot;;
  }
  return 0;
}

This works because if considers any nonzero value as "truth". You may find references in to "truthy" values and "truthiness" as a way to describe this concept.

答案2

得分: -1

在cppreference中,如果传递的char值未定义,std::islower和std::isupper函数的行为未定义。正如所指出的,为了安全起见,应将其转换为无符号char。

无论如何,您的问题出在std::islower和std::isupper函数返回一个整数值,所以不应将其作为布尔值(true、false)进行比较。然后,您应该将结果与整数值0进行比较。

英文:

In cppreference, the behavior of the std::islower and std::isupper functions is undefined if the passed char value. As indicated, it should be converted to an unsigned char for safety.

Anyway, your problem comes from the fact that the std::islower and std::isupper functions return an integer value, so you should not compare it as a boolean value (true, false). Then, you should compare the result with the integer value 0.

huangapple
  • 本文由 发表于 2023年2月27日 03:26:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定