C++ 打破内部循环并不意味着';不要终止循环

C++ 打破内部循环并不意味着';不要终止循环,c++,loops,if-statement,while-loop,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 numPa

该代码是一个四个字符的密码生成器。我有一个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;
}
#包括
使用名称空间std;
int
主要(){
char-char1;
char-char2;
char-char3;
char-char4;
int numpasswords生成=0;

cout如果您想提前停止,请将其放入函数中,而不是使用
break
使用
return

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

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

#包括
无效生成密码(常量大小限制){
字符密码[5];
大小\u t计数=0;
密码[4]=0;
密码[0]=“!”;

while(密码[0]<代码>中断< /代码>语句只从最内层循环中跳出,而不是从所有四个循环中出来。第三个循环向下继续进行下一个迭代,<代码> NuSuthWordDS生成< /C> > 11,并且条件不再满足。在这里您想做的事情中,JavaScript与C或C++没有任何不同。bug并用您熟悉的语言解决此问题。创建一个包含所有有效字符的数组,然后使用range for循环使用随机函数来选择您想要的结果,这不是很容易吗?如果您希望我扩展我的回答,我可以,但它不能解决您的问题。它只是提供了一个替代方案解决方案。谢谢@IgorTandetnik!我刚刚注意到在多做了一点测试之后。在我头上飞了一会儿!@TheGrandJ An数组会更有效(而且肯定更容易),但我想看看我是否可以将自己限制在循环语句中。