C ++中意外的无限循环

unexpected infinite loop in c++

我在做一个项目,任务是做一个注册功能 因此,当我在密码功能中尝试多个测试用例时,当我尝试输入无效密码而不给我重新输入密码的机会时,我陷入了无限循环。 提前致谢...

#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 does not remove the newline from the stream. This means the next call to std::istream::get immediately finds the newline and reads nothing into the buffer, sets the failbit, and returns. Because the steream is now in the fail state all subsequent reads instantly fail. You could cin.ignore() the newline, but a more direct approach is to use std::istream::getline 因为它会为您删除换行符。

始终在 IO 事务后测试流状态以确保它成功。

旁注:

优先使用 std::stringstd::getline 来处理原始字符数组和使用它们的函数。如果提供的缓冲区太小,std::istream::getstd::istream::getline 都会将流标记为失败。提供给 std::getlinestring 将调整大小,因此只要您有剩余的动态存储空间,缓冲区溢出运行和为防止它们而引发的错误都不是问题。如果系统无法为 string 分配足够大小的缓冲区,则会抛出异常以提醒您注意该问题。

更喜欢使用字母,例如 'A' 到原始 ASCII 代码。一方面,即使是最原始的程序员也能立即识别出 'A" 的意图,并且始终存在实现不使用 ASCII 作为默认编码的可能性。