我非常擅长编码而且我试图让我的头发试图让这个代码循环(直到满足正确的标准...大写/小写字母和一个数字)我是在做什么循环在正确的地方??
非常感谢您收到任何帮助..
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
main()
{
int i;
int hasUpper, hasLower, hasDigit;
char password[20];
hasUpper = hasLower = hasDigit = 0; // initialising these three variables to false (o)
printf("Please enter a alpha numeric password with upper and lower case\n");
scanf(" %s", password);
do {
for (i = 0; i < strlen(password); i++) {
if (isdigit(password[i])) {
hasDigit = 1;
continue;
}
if (isupper(password[i])) {
hasUpper = 1;
continue;
}
if (islower(password[i])) {
hasLower = 1;
continue;
}
}
printf("Not so good, try again!");
scanf(" %s", password);
} while ((hasDigit) && (hasLower) && (hasUpper)==1);
// This loop will only execute if all three variables are true
if ((hasUpper) && (hasLower) && (hasDigit)) {
printf("Great password!");
}
return 0;
}
答案 0 :(得分:2)
您的while
条件有问题,每次尝试后变量都需要清除,打印失败需要检查。此外,将scanf()
移动到循环的开头会使事情变得更容易,并且在初始输入时无需额外的循环。
#include <stdio.h>
#include <string.h>
#include <stdbool.h> // Use for boolean types
int main(int argc, const char argv[]) { // Correct function signature
int i = 0, plen = 0;
bool hasUpper = false, hasLower = false, hasDigit = false; //Change types to boolean
char password[20] = {0}; // Initialise string with all '\0'
printf("Please enter an alpha-numeric password with upper and lower case\n");
do {
hasUpper = false; // Clear boolean variables for each new password
hasLower = false;
hasDigit = false;
scanf("%s", password);
password[19] = 0; // ensure string is correctly terminated with '\0'
plen = strlen(password); // Get the string length *once* per new password
for (i = 0; i < plen; ++i) {
if (isdigit(password[i])) { // Use 'if...else' in place of 'continue'
hasDigit = true;
}
else if (isupper(password[i])) {
hasUpper = true;
}
else if (islower(password[i])) {
hasLower = true;
}
}
if ((!hasDigit) || (!hasLower) || (!hasUpper)) { // Check the booleans before printing fail message
printf("Not so good, try again!");
for (i = 0; i < 20; ++i) {
password[i] = 0; // Clear the string with all '\0'
}
}
} while ((!hasDigit) || (!hasLower) || (!hasUpper)); // Correct the logic here
printf("Great password!"); // Remove the unneeded boolean check here
return 0;
}
我还会考虑将if...continue
模式替换为if...else if
,因为使用continue
是不好的做法。
答案 1 :(得分:0)
由于一些逻辑问题,代码无法正常工作
首先,即使所有值(hasDigit
,hasLower
和hasUpper
)都为1(while中的条件错误),do while循环仍将继续。
即使所有这些都是1,您也会打印“不太好”的声明。
还有一个问题是,如果您输入一次错误的密码,则会将值分配给三个变量,但它们不会重置为0,因此,当您输入新密码时,这三个变量的值将是它在前一个循环中获得的值(也就是说,如果在您之前的密码中,如果其中任何一个已设置为1,则即使下一个密码,该值仍将保持为1)。
现在,这是我修复错误的代码
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int i;
int hasUpper,hasLower,hasDigit;
char password [20];
hasUpper=hasLower=hasDigit=0; // initialising these three variables to false (o)
printf("Please enter a alpha numeric password with upper and lower case\n");
scanf(" %s", password);
do
{
hasUpper=hasLower=hasDigit=0; // you need to initialize them to 0 here as well
for(i=0;i<strlen(password); i++)
{
if (isdigit(password[i]))
{
hasDigit = 1;
}
if (isupper(password[i]))
{
hasUpper = 1;
}
if (islower(password[i]))
{
hasLower = 1;
}
}
if( ( (hasUpper) && (hasLower) && (hasDigit) ) !=1 ) // with this if statement, only if all the criteria are not met, the printf is executed
{
printf("Not so good, try again!");
scanf(" %s", password);
}
}while( ( (hasDigit) && (hasLower) && (hasUpper) ) !=1 ); // loop continues as long as all of them are not 1
// This statement will only execute if all three variables are true
printf("Great password!");
return 0;
}
请注意,我已从
中删除了if条件if ((hasUpper)&&(hasLower)&&(hasDigit))
{
printf("Great password!");
}
这是因为只有当你输入一个好密码时,程序才会出现do while循环,因此不再需要if语句,只需要printf即可。