簡體   English   中英

C - Do While 循環中的無限循環

[英]C - Endless Cycle in a Do While Loop

我需要循環循環直到它結束,但是即使 i>=k 編譯器繼續循環並且我無法退出無限循環。 這個片段有什么問題?

int i=0;
User signed_up[k]
char input_user[15];
bool stop_cycle=false;

do {
    printf("\n\n%*sUsername: ", 26, "");
    fflush(stdin);
    scanf("%s", input_user);
    stop_cycle=true;

    for (i=0;i<k;i++) {
        if (strcmp(input_user, signed_up[i].username)==0) {
            printf("\n%*sUsername inserito gi%c presente nel database!", 26, "", 133);
        }
        stop_cycle=false;
    }
} while (!stop_cycle);

編輯:k 是一個計數器,每次運行時都會在子程序注冊中增加。 在代碼的這一點上,它可能是 0、1 等。我想要實現的是每次我插入一個已經存在的用戶名時都要求再次插入它,直到數組中的搜索結束。

你到底想在這里做什么?

您的代碼所采取的步驟既在每次執行迭代中將 i 變量重置為 0,又在每次迭代中將 stop_cycle 設置為 true 和 false。

每個 do 迭代的 for 循環從 i=0 運行到 i < k。

您可以通過每次將 stop cycle 設置為 false 來結束 do,因此 while 永遠不會觸發。

嘗試將其運行為:

printf("\n\n%*sUsername: ", 26, "");
fflush(stdin);
scanf("%s", input_user);

for (i=0;i<k;i++) {
    if (strcmp(input_user, signed_up[i].username)==0) {
        printf("\n%*sUsername inserito gi%c presente nel database!", 26, "", 133);
    }
}

或者

int i=0;
User signed_up[k]
char input_user[15];
bool stop_cycle=true;

do {
    printf("\n\n%*sUsername: ", 26, "");
    fflush(stdin);
    scanf("%s", input_user);
    stop_cycle=true;

    for (i=0;i<k;i++) {
        if (strcmp(input_user, signed_up[i].username)==0) {
            printf("\n%*sUsername inserito gi%c presente nel database!", 26, "", 133);
            stop_cycle=false;
        }
    }
} while (stop_cycle);

請注意停止循環變量的位置變化及其在第二個版本中的值。

tl;博士

int i=0;
User signed_up[k];
char input_user[15];
bool stop_cycle=false;

do {
    printf("\n\n%*sUsername: ", 26, "");
    fflush(stdin);
    scanf("%s", input_user);
    stop_cycle=true;

    for (i=0;i<k;i++) {
        if (strcmp(input_user, signed_up[i].username)==0) {
            printf("\n%*sUsername inserito gi%c presente nel database!", 26, "", 133);
            stop_cycle=false;
        }
    }
} while (!stop_cycle);

更長的解釋

如果這是你的情況...

所以你想要求他們輸入用戶名。

如果用戶名存在,您想再次詢問他們(輸入不同的用戶名),並打印“ Username inserito già presente nel database!

您希望繼續這樣做,直到用戶輸入了不在“數據庫”中的用戶名。

for循環的目的是測試用戶名( input_user )是否已經在“數據庫”(即數組, signed_up )中。

變量k是對signed_up數組中User對象數量的計數。

因此, for循環遍歷signed_up數組中的所有值,以測試input_user是否與數組中User對象的User username成員匹配。

如果input_userUser的現有username匹配,則您希望在do while循環中繼續循環。

stop_cycle設置為true將停止循環。

然后...

你的問題是你只想設置stop_cycle=false; input_user匹配username 那是您停止循環的唯一時間(即stop_cycle=false;

錯誤是你把stop_cycle=false; if語句之外。 它應該在if語句中。 只要用戶名已經存在,這將繼續“循環”。

所以這段代碼有效:

int i=0;
User signed_up[k];
char input_user[15];
bool stop_cycle=false;

do {
    printf("\n\n%*sUsername: ", 26, "");
    fflush(stdin);
    scanf("%s", input_user);
    stop_cycle=true;

    for (i=0;i<k;i++) {
        if (strcmp(input_user, signed_up[i].username)==0) {
            printf("\n%*sUsername inserito gi%c presente nel database!", 26, "", 133);
            stop_cycle=false;
        }
    }
} while (!stop_cycle);

其他建議

除了其他人的建議,我的建議是:

stop_cycle重命名為username_exists ,將k重命名為num_users

所以代碼看起來像這樣:

int i=0;
User signed_up[num_users];
char input_user[15];
bool username_exists=false;

do {
    printf("\n\n%*sUsername: ", 26, "");
    fflush(stdin);
    scanf("%s", input_user);
    username_exists=false;

    for (i=0;i<num_users;i++) {
        if (strcmp(input_user, signed_up[i].username)==0) {
            printf("\n%*sUsername inserito gi%c presente nel database!", 26, "", 133);
            username_exists=true;
        }
    }
} while (username_exists); 

這清楚地表明您的for循環正在測試以查看用戶名是否存在,並且清楚地表明只要用戶名存在,您的do循環就會繼續。 它還清楚地表明, k代表用戶數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM