c++ - 跳出内部循环并不会终止循环

标签 c++ loops if-statement while-loop

该代码是一个四字符密码生成器。我有一个 if/else 中断系统设置,但我不明白为什么它不起作用。循环生成超过限制,直到用尽所有选项,我很难正确调试。我对 javascript 和 HTML 以外的任何东西都很陌生,所以任何帮助都会很有用。谢谢!

#include <iostream>
using namespace std;

int
main ()  {
  char char1;
  char char2;
  char char3;
  char char4;
  int numPasswordsGenerated = 0;

  cout << "Password Generator:" << endl;

  /* Generates four-character passwords (excludin digits) by exhausting all character
     options between '!' and '~' starting with the fourth character. Once the fourth 
     character exhausts all options, the third character increases by the next character 
     in the binary lineup, then continuing with the fourth character. This process continues 
     until the numPasswordsGenerated integer reaches the desired number, or the password 
     output becomes "~~~~"; whichever comes first.
   */
  char1 = '!';
  while (char1 <= '~')    {
      char2 = '!';
      while (char2 <= '~')    {
          char3 = '!';
          while (char3 <= '~')    {
              char4 = '!';
              while (char4 <= '~') {
                  // cout << char1 << char2 << char3 << char4 << endl;
                  numPasswordsGenerated++;                  //!*FIXME*!
                  cout << numPasswordsGenerated << endl;    //DEBUG DELETE
                  if (numPasswordsGenerated == 10)
                    break;
                    
                  char4++;
                  
              }
          char3++;
              
          }
      char2++;
          
      }
      char1++;
      
  }

  return 0;
}

最佳答案

如果你想提前停止,把它放在一个函数中,而不是 break 使用 return

请记住,break 只会跳出您所在的循环,而不是函数范围内的所有循环。

这是一个修改后的版本,使用 argv 对限制进行了更灵活的输入安排:

#include <iostream>

void generatePasswords(const size_t limit) {
  char password[5];
  size_t count = 0;

  password[4] = 0;

  password[0] = '!';

  while (password[0] <= '~') {
    password[1] = '!';

    while (password[1] <= '~') {
      password[2] = '!';

      while (password[2] <= '~') {
        password[3] = '!';

        while (password[3] <= '~') {
          password[3]++;

          std::cout << password << std::endl;

          if (++count >= limit) {
            return;
          }
        }

        password[2]++;
      }

      password[1]++;
    }

    password[0]++;
  }
}

int main(int argc, char** argv) {
  std::cout << "Password Generator:" << std::endl;

  generatePasswords(argc >= 2 ? atoi(argv[1]) : 1000);

  return 0;
}

注意使用 char password[5] 而不是一堆不相关的字符。

关于c++ - 跳出内部循环并不会终止循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64201340/

相关文章:

c++ - 缺少类型说明符 - 假定为 int。注意 c++ 不支持 default-int

python - python 中 epsilon 为 0.0001 的递归平方根循环

javascript - 如何让多个模态框出现在 while 循环中?

Javascript 检查文本框中是否有某个单词

java - 有没有更好的方法来为 java 编写这段代码?还是让它更干净?

r - 应用 if else 条件在 r 中创建新列

c++ - 使用 OpenGL 在 Qt 项目中设置错误

c++ - 在 Windows 上使用多线程文件 IO 的 SHARING_VIOLATION

c++ - string::at 和 string::operator[] 有什么区别?

javascript - React Typescript 映射循环接口(interface)错误