gpt4 book ai didi

c++ - if 语句不比较我的字符串 c++ 中的数值

转载 作者:行者123 更新时间:2023-11-30 05:03:51 25 4
gpt4 key购买 nike

作业是让用户输入具有一定要求的密码,例如长度、大写/小写字母和数字。当我尝试运行在字符串中添加数字数量的 for 循环时,它似乎跳过了 if 语句。

 #include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;

int main()
{
const int LENGTH = 13;
char str[LENGTH];
int size = 0, k = 0, j = 0, r = 0;

cout << "Enter a password." << endl;
cout << "Must meet these requirements:" << endl;
cout << "At least 10 and at most 12 characters." << endl;
cout << "Any characters entered after 12 will be ignored." << endl;
cout << "At least 2 lower case, and 2 upper case letters." << endl;
cout << "At least 3, but no more than 5 numerical digits." << endl;
cout << "No white spaces." << endl;

cin.getline(str, LENGTH);

//Validating numerical values.
for (int z=0; z < 12; z++)
{
if (str[z] >=0 && str[z] <=10)
{
r++;
}
}

if (r<3 || r>5)
{
cout << "Must have at least 3 but no more than 5 numerical digits."<<endl;
exit (EXIT_FAILURE);
}

最佳答案

在您的代码中,您正在检查 0 到 10 之间的 ASCII 值,但您想要检查“0”和“9”之间的字符。您可以在 ASCII 表上检查这些字符的值并使用这些值:

if(str[z] >= 60 && str[z] <= 71)

或使用字符:

if(str[z] >='0' && str[z] <='9')

关于c++ - if 语句不比较我的字符串 c++ 中的数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49215732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com