A
A
Alexander2020-12-21 12:41:52
C++ / C#
Alexander, 2020-12-21 12:41:52

How to not continue executing the body of the loop for certain user input?

I'm writing a small console program for myself (password generator). I have this piece of code:

else if (answer == "NO" || answer == "no" || answer == "No" || answer == "nO")
    {
      system("cls");

      cout << "Generating passwords... " << endl << endl << endl;

      for (int i = 0; i < quantity; i++)
      {
        cout << "\nNumber of characters of the password №" << i + 1 << ": ";
        cin >> length;

        if (length < 8)
          break;

        else
        {
          cout << endl;

          for (int j = 0; j < length; j++)
          {
            cout << possible_characters[rand() % size];
          }

          cout << endl << endl;
        }
      }
    }


Before this code, the user chooses the number of passwords and whether he wants all passwords to have the same number of characters. If the answer is no, then you need to enter your own number of characters for each password.

Let's say the number of passwords is 2 and the answer is no. Then on the screen:

Enter the number of characters for password No. 1: (input)

And if the number of characters is less than 8, then an error pops up and you are asked to enter the number of characters for the first password again. When the input is already correct, the generated password is displayed, and then the program proceeds to the second password. I think the meaning is clear.

How can I implement this? Tell me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
QWERTYUIOPas, 2020-12-21
@QWERTYUIOPas

Decision:

while(1){
 //code
 int answer;
 cin<<a;
 if(a<8){
  break;
 }
 std::cout>>"Wrong answer!!!\n"
}

In one of the pieces of code, it would be more correct to write:
if(answer in ["NO","no"",No"","nO"]){
 //code...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question