[英]Problems with a Do-While Loop in C
我在編碼方面非常陌生,我想盡辦法使此代碼循環(直到滿足正確的標准。.大寫/小寫字母和數字)才能解決。我是否將do while循環放在正確的位置??
非常感謝您提供的任何幫助。
#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;
}
你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
使用則不好。
由於一些邏輯問題,該代碼無法正常工作。
首先,即使所有值( 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 ((hasUpper)&&(hasLower)&&(hasDigit))
{
printf("Great password!");
}
這是因為僅當您輸入“良好密碼”時程序才從do while循環中退出,因此不再需要該if語句,僅用printf就足夠了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.