The first part inside the "If" statement works perfectly. The only part I am having trouble with is the "else" statement. I was trying to get it to generate an "x" amount of passwords and get those passwords to be at a length defined by the user. The problem is that it will only spew out one password. The rest have the "Password(x): " but nothing beside it. I have been through the code I don't know how many times. Would someone be able to help me? I'd greatly appreciate it. Thank you

#include <iostream>
#include <ctime>

using namespace std;

int main () {
    
    int amount, length;
    cout << "Password Generator v1" << endl;
    cout << "How many passwords would you like to generate?" << endl;
    cin >> amount;
    cout << "How long would you like the passwords to be?" << endl;
    cin >> length;
    cout << endl;
    cout << endl;
    srand( time(NULL) );
    if (amount == 1) {
              cout << "Your generated password is: ";
              while (length > 0) {
              cout << (rand() % 9) + 1;
              length--;
              }
}
    
    else {
         cout << "**Passwords List**" << endl;
         cout << endl;
         for (int r = 0; r <= amount; r++) {
         cout << "Generated password(" << r << "): ";
         //Creates password length
             while (length > 0) {
              cout << (rand() % 9) + 1;
              length--;
                       }
                       // Adds spacing after each password
                       cout << endl;
                       cout << endl;
              }
        }
   cout << endl;
   system("pause");
   return 0;
}

You have to redeclare a size for length other than zero every time through the for loop as it goes to zero in the while loop.

-Deleted-

-Deleted-

int LoopControl = 15, LoopBreak = 5, j = LoopBreak;
for (int i = 1; i <= LoopControl; i++) {
printf("%d", i);
if (--j == 0){
j = --LoopBreak;
printf("\n");
} end of FOR loop

This is to print the number.
============================
<FAKE SIGNATURE>

It is a very simple problem.

On line 33, there is length--; . After the first loop in the for scope, length is already 0. In the rest turns, it cannot return true in (length > 0) , as length has already been reduced to 0.

It is better to declare another int j or use the following one:

for (int j=0; j<length; j++) {
// ...
}

Other advice: It is better to change (amount == 1) into (amount <= 1) .


And the indent of your code seems to have some problem. If you are using Dev-C++, I recommend you to turn off smart tab.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.