簡體   English   中英

c++ 中的意外無限循環

[英]unexpected infinite loop in c++

我正在一個項目中工作,任務是注冊 function 所以當我在密碼 function 中嘗試幾個測試用例時,當我嘗試輸入無效密碼而沒有機會重新輸入密碼時,我陷入了無限循環. 提前致謝...

#include <iostream>
#include <string>
#include <cstring>
#include <conio.h>
#include <stdio.h>
using namespace std;
#define size 40
int main(){
    bool flag = true;
    while (flag){
        bool flagS = false, flagN = false; int count = 0;
        char password[size] = { 0 };
        cout << "Type your Password .....\n Note :: Must be more than 8 characters including at least one number and one special character...\n";
        cin.get(password, size);
        count = strlen(password);
        for (int z = 0; z < count; z++){
            if (password[z] >= 48 && password[z] <= 57)
                flagN = 1;
            if ((password[z] >= 33 && password[z] <= 47) || (password[z] >= 58 && password[z] <= 64))
                flagS = 1;
        }

        if ((flagS == 1) && (flagN == 1) && (count >= 8))
        {
            cout << "Valide Password ...\nCongrats!! ..you created a NEW account.." << endl;
            flag = false;
        }
        else
        {
            cout << "invalide password..\nPlease try again..\n";
            flag = true;
        }
    }
}

您必須在while循環內寫入cin 這將解決您的問題:

#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
#define size 40
int main(){
    bool flag = true;
    bool flagS = false, flagN = false; int count = 0;
    char password[size] = { 0 };

    cout << "Type your Password .....\n Note :: Must be more than 8 characters including at least one number and one special character...\n";

    while (cin >> password && flag){

        count = strlen(password);
        for (int z = 0; z < count; z++){
            if (password[z] >= 48 && password[z] <= 57)
                flagN = true;
            if ((password[z] >= 33 && password[z] <= 47) || (password[z] >= 58 && password[z] <= 64))
                flagS = true;
        }

        if ((flagS) && (flagN) && (count >= 8))
        {
            cout << "Valide Password ...\nCongrats!! ..you created a NEW account.." << endl;
            flag = false;
        }
        else
        {
            cout << "invalide password..\nPlease try again..\n" << "Type your Password .....\n Note :: Must be more than 8 characters including at least one number and one special character...\n";;
            flag = true;
        }
    }
}

std::istream::get不會從 stream 中刪除換行符。 這意味着對std::istream::get的下一次調用會立即找到換行符,並且不會將任何內容讀入緩沖區,設置失敗位並返回。 因為流現在處於失敗 state 所有后續讀取立即失敗。 您可以cin.ignore()換行符,但更直接的方法是使用std::istream::getline因為它會為您刪除換行符。

始終在 IO 事務之后測試 stream state 以確保其成功。

邊注:

更喜歡使用std::stringstd::getline來代替原始字符 arrays 和使用它們的函數。 如果提供的緩沖區太小, std::istream::getstd::istream::getline都將 stream 標記為失敗。 提供給std::getlinestring將被調整大小,因此只要您有剩余的動態存儲空間,緩沖區溢出以及為防止它們而引發的錯誤都不是問題。 如果系統無法為string分配足夠大小的緩沖區,則會引發異常以使您意識到問題。

更喜歡使用字母,例如'A'而不是原始 ASCII 代碼。 一方面,即使是最原始的程序員也可以立即識別'A"的意圖,並且始終存在實現不使用 ASCII 作為默認編碼的可能性。

暫無
暫無

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

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