gpt4 book ai didi

c - 循环回到用户输入验证 C

转载 作者:太空宇宙 更新时间:2023-11-04 03:59:19 25 4
gpt4 key购买 nike

我有以下方法来生成必须满足要求的字符串,但如果不满足验证要求,我需要某种子句来循环回用户输入。我知道在 Java 中有后续读取,但我我不确定 C 中所需的代码是什么,如果任何人都能发现错误,我也不认为我的 else if 语句语法正确。一些提示或建议会有所帮助,谢谢。

void validatePass()
{
FILE *fptr;
char password[MAX+1];
int iChar,iUpper,iLower,iSymbol,iNumber,iTotal,iResult,iCount;

//shows user password guidelines
printf("\n\n\t\tPassword rules: ");
printf("\n\n\t\t 1. Passwords must be at least 9 characters long and less than 15 characters. ");
printf("\n\n\t\t 2. Passwords must have at least 2 numbers in them.");
printf("\n\n\t\t 3. Passwords must have at least 2 uppercase letters and 2 lowercase letters in them.");
printf("\n\n\t\t 4. Passwords must have at least 1 symbol in them (eg ?, $, £, %).");
printf("\n\n\t\t 5. Passwords may not have small, common words in them eg hat, pow or ate.");

//gets user password input
printf("\n\n\t\tEnter your password following password rules: ");
scanf("%s", &password);


iChar = countLetters(password,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal);

if(iUpper < 2)
{
printf("Not enough uppercase letters!!!\n");


}
else if(iLower < 2)
{
printf("Not enough lowercase letters!!!\n");


}
else if(iSymbol < 1)
{
printf("Not enough symbols!!!\n");


}
else if(iNumber < 2)
{
printf("Not enough numbers!!!\n");


}
else if(iTotal < 9 && iTotal > 15)
{
printf("Not enough characters!!!\n");


}

iResult = checkWordInFile("dictionary.txt",password);

if( iResult == gC_FOUND )
{
printf("\nFound your word in the dictionary");
}
else if
{
printf("\nCould not find your word in the dictionary");
}

iResult = checkWordInFile("passHistory.txt",password);
else if( iResult == gC_FOUND )
{
printf("\nPassword used");
}
else if
{
printf("\nOk to use!");
}
else
{
printf("\n\n\n Your new password is verified ");
printf(password);
}
//writing password to passHistroy file.


fptr = fopen("passHistory.txt", "w"); // create or open the file
for( iCount = 0; iCount < 8; iCount++)
{
fprintf(fptr, "%s\n", password[iCount]);
}

fclose(fptr);

printf("\n\n\n");
system("pause");


}//end validatePass method

最佳答案

使用 goto。这是为数不多的少数情况之一。

这是一个例子。正如您所看到的,它比 while(0) 干净得多,并且也会使编译器对 -Wall 的提示更少!

// Returns whether or not the condition failed, printing the
// given error if it did.
static bool verifyThat(bool condition, const char* error) {
if(!condition) printf("%s", error);
return !condition;
}

void validatePass()
{
FILE *fptr;
char password[MAX+1];
int iChar,iUpper,iLower,iSymbol,iNumber,iTotal,iResult,iCount;

//shows user password guidelines
printf("\n\n\t\tPassword rules: ");
printf("\n\n\t\t 1. Passwords must be at least 9 characters long and less than 15 characters. ");
printf("\n\n\t\t 2. Passwords must have at least 2 numbers in them.");
printf("\n\n\t\t 3. Passwords must have at least 2 uppercase letters and 2 lowercase letters in them.");
printf("\n\n\t\t 4. Passwords must have at least 1 symbol in them (eg ?, $, £, %).");
printf("\n\n\t\t 5. Passwords may not have small, common words in them eg hat, pow or ate.");

get_user_password:

printf("\n\n\t\tEnter your password following password rules: ");
scanf("%s", &password);

iChar = countLetters(password,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal);
iUpper = ...
iLower = ...
iSymbol = ...
iNumber = ...
iTotal = ...

if(verifyThat(iUpper >= 2, "Not enough uppercase letters!!!\n")
|| verifyThat(iLower >= 2, "Not enough lowercase letters!!!\n")
|| verifyThat(iSymbol >= 1, "Not enough symbols!!!\n")
|| verifyThat(iNumber >= 2, "Not enough numbers!!!\n")
|| verifyThat(iTotal >= 9, "Not enough characters!!!\n")
|| verifyThat(iTotal <= 15, "Too many characters!!!\n"))
goto get_user_password;

iResult = checkWordInFile("dictionary.txt", password);

if(verifyThat(iResult != gC_FOUND, "Password used."))
goto get_user_password;

printf("Your new password is verified.");
}

关于c - 循环回到用户输入验证 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13518495/

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