[英]islower function to output boolean value of 0 or 1 depending on input
I tried the following but am not getting 0 or 1. The question is how do I make this program print out a boolean value of 0 or 1 without using conditional statement in C++. 我尝试了以下操作,但没有得到0或1。问题是如何在不使用C ++中的条件语句的情况下使该程序打印出布尔值0或1。
char input;
char output;
cout << "Input A Character: \n";
cin >> input;
input = islower(input);
cout << input;
To get either 0
or 1
, you can use the double !
要获得
0
或1
,可以使用double !
operation: 操作:
cout << !!input;
!!
is a common idiom to normalize boolean values. 是标准化布尔值的常见习语。 It yields
0
when the value is 0
and 1
when the value is non- 0
. 它产生
0
时的值是0
和1
时的值是非0
。
Some people don't like the double negation, and you can get the same result with the !=
equality operator: 有些人不喜欢双重否定,使用
!=
等于运算符可以得到相同的结果:
cout << (input != 0);
Use an int
instead of a char
: 使用
int
代替char
:
int result = islower(input);
cout << result;
Note that islower is only guaranteed to return "0" or "something that isn't 0". 注意,仅保证islower返回“ 0”或“非0的值”。 You can fix that by writing
cout << (result ? 1 : 0)
, but I'm not sure if that is disallowed by your requirements. 您可以通过编写
cout << (result ? 1 : 0)
来解决该问题,但是我不确定您的要求是否不允许这样做。
islower
should return 0 when the character is NOT a lowercase letter. 如果字符不是小写字母,则
islower
应该返回0。
Remember that only 0 is false
. 请记住,只有0为
false
。 Even if the function returns -1, it's still a true
value. 即使函数返回-1,它仍然是一个
true
值。
Check out the reference here . 在此处查看参考。
EDIT: 编辑:
You can try using input = 1 && islower(input);
您可以尝试使用
input = 1 && islower(input);
. 。 This will force you
true
value to become 1. 这将迫使您的
true
价值变为1。
Both 0 and 1 are non-printable characters, that's why you don't see the output. 0和1都是不可打印的字符,这就是为什么看不到输出的原因。 If you redirect the output to a file and open with a hex editor you'll see the result.
如果将输出重定向到文件并使用十六进制编辑器打开,则会看到结果。
Store the result in a boolean, or cast the result: cout << (bool) input;
将结果存储为布尔值,或将结果
cout << (bool) input;
为: cout << (bool) input;
Every value except 0 == true. 除0外的每个值== true。 0 == false.
0 ==错误。
you can store the output of islower(input)
in an integer variable, say value
, and cout << value
您可以将
islower(input)
的输出存储在一个整数变量中,例如value
和cout << value
int value = islower(input);
cout << input;
#include <iostream>
using namespace std;
int
main()
{
char input;
cout << "Input A Character: \n";
cin >> input;
unsigned int bIsLower = islower(input);
cout << "input: " << input << ", bIsLower: " << bIsLower << endl;
return 0;
}
Input A Character:
输入一个字符:
a一种
input: a, bIsLower: 2输入:a,bIsLower:2
Input A Character:
输入一个字符:
A一种
input: A, bIsLower: 0输入:A,bIsLower:0
Input A Character:
输入一个字符:
zž
input: z, bIsLower: 2输入:z,bIsLower:2
To print a boolean value use a variable of type bool . 要打印布尔值,请使用bool类型的变量。
char input;
bool output;
// ^^^^
cout << "Input A Character: \n";
cin >> input;
output = islower(input);
// ^^^^^^ Store result in a bool variable (otherwise why have output)
cout << output;
// ^^^^^^^ Now print it.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.