0

im coding a password generator, but i have to use a delay if i want the output to be diferent, i have the code (a bit messy tho).

//Variables
int menu = 0, prec = 0, digit = 0;
char password;

//Menú
cout << "##############################" << endl;
cout << "#  Generador de Contraseñas  #" << endl;
cout << "#----------------------------#" << endl;
cout << "#            MENÚ            #" << endl;
cout << "#   (1) Generador   -        #" << endl;
cout << "#   (2) Historial   -        #" << endl;
cout << "#   (3) Contraseñas -        #" << endl;
cout << "#       guardadas   -        #" << endl;
cout << "#                            #" << endl;
cout << "##############################" << endl;
cin >> menu;

while (menu == 1)
{
    cout << "¡Bienvenido al generador de contraseñas! Introduce la longitud de la contraseña (entre 8 y 32)" << endl;
    cin >> prec;
    if (prec <= 8 || prec <= 32)
    {
        cout << "Estamos calculando tu contraseña" << endl;
        while (digit++ < prec)
        {
            srand(time(NULL));
            password = (rand() % 74) + 48;
            cout << password;
            Sleep(1000);

        }
        cout << " es la contraseña generada."<< endl;
        digit = 0;
    }
    else
    {
        cout << "Has puesto un numero que no esta entre 8 y 32, introduce otro" << endl;
        cin >> prec;
    }
}

while (menu == 2)
{

}

while (menu == 3)
{

}
system("pause");

}

Im a bit new in c++, so if there is any way to do it better, im open to suggestions. im using windows by the way

  • 1
    Call srand one time, only; outside the loops, and remove Sleep. – zdf Jan 25 '20 at 13:10
  • 1
    There are better random numbers in c++11 but you still want to seed them 1 time instead of in a loop. – drescherjm Jan 25 '20 at 13:57
  • Does this answer your question? [How to generate different random numbers in a loop in C++?](https://stackoverflow.com/questions/4926622/how-to-generate-different-random-numbers-in-a-loop-in-c) – drescherjm Jan 25 '20 at 14:04
  • Why would you call `srand` more than once in a program? (Assuming you are not interested in the better alternatives in modern C++.) – Eljay Jan 25 '20 at 14:43
  • "if there is any way to do it better" - Better IMHO would be to *not* use `srand`/`rand` at all, but use the [modern random facilities](https://en.cppreference.com/w/cpp/numeric/random) available since C++11. I think you'll find this instructive: [rand() Considered Harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful). – Jesper Juhl Jan 25 '20 at 15:07

0 Answers0