2021年3月6日星期六

Why into an infinite loop?

I have created a program where the user enters a password according to some specifications. The whole program is already made and is done, but what I cannot figure out is why the program goes into an infinite loop when the password meets the requirements. It'll go on forever saying "enter password" and "password is valid". I've tried changing the loop in the main function and also tried changing the loop in isValid function, have tried putting break as well but have come up empty handed. Any clues/hints as to why this could be happening? Thanks!

#include <iostream>  #include <cstring>    using namespace std;    void displayRequirements();  void displayResult (char[]);  bool hasUpper (char []);  bool hasLower (char []);  bool hasDigit (char []);  bool hasLength(char []);  bool isValid (char []);    int main ()    {      displayRequirements();            const int size = 7;      char password [size];      int length;            length = strlen(password);            while (length < size)      {          cout << endl;          cout << "Enter a password: ";          cin.getline(password, size);          length = strlen(password);                    isValid (password);      }            cout << endl;            return 0;  }    void displayRequirements()  {      cout << "Password Requirements: " << endl;      cout << "   - The password should be at least 6 characters long." << endl;      cout << "   - The password should contain at least one uppercase." << endl;      cout << "   and at least one lowercase letter." << endl;      cout << "   - The password should have at least one digit." << endl;  }    bool hasUpper (char input[])  {      for (int count = 0; count < strlen(input); count++)      {          if (isupper(input[count]))          {              return true;          }      }      return false;  }    bool hasLower (char input[])  {      for (int count = 0; count < strlen(input); count++)      {          if (islower(input[count]))          {              return true;          }         }      return false;  }      bool hasDigit (char input[])  {         for (int count = 0; count < strlen(input); count++)      {          if (isdigit(input[count]))          {              return true;          }      }      return false;  }    bool hasLength(char input[])  {         int size = 6;      int length = strlen(input);            if (size == length)      {          return true;      }      return false;  }    void displayResult(char input[])  {      if (!hasLength(input))      {          cout << "It should be at least 6 characters long." << endl;      }            if (!hasDigit(input))      {          cout << "It should have at least one digit." << endl;      }            if (!hasLower(input))      {          cout << "It should contain at least one lowercase letter." << endl;      }            if (!hasUpper(input))      {          cout << "It should contain at least one uppercase letter." << endl;      }  }    bool isValid (char input[])  {      int length = strlen(input);        for (int count = 0; count < 1; count++)      {          if (input[length] != hasUpper(input) || hasLower(input) || hasDigit(input) || hasLength(input))          {              cout << endl;              cout << "The password is invalid." << endl;              cout << endl;              displayResult(input);          }                    else if (input[length] == hasUpper(input) || hasLower(input) || hasDigit(input) || hasLength(input))          {              cout << endl;              cout << "Password is valid" << endl;\              break;          }      }        return 0;  }  
https://stackoverflow.com/questions/66511739/why-into-an-infinite-loop March 07, 2021 at 07:28AM

没有评论:

发表评论