私はC++でループと条件を学習しようとしていますので、ユーザのランダムパスワードを生成するプログラムを書くことにしました。なんらかの理由で、コードは1/5倍になるかもしれませんが、残りの部分では "Exit with Non-Zero Status"と表示されます。 おかげで、 エビンランダムパスワードジェネレータのC++ループエラー "非ゼロ状態で終了"
#include <iostream>
#include <cstdlib>
#include <chrono>
#include <thread>
#include <time.h>
using namespace std;
int main()
{
using namespace std::this_thread;
using namespace std::chrono;
// Vars
string lett;
int input;
string password("");
string lettArray [] = {"a", "b", "c", "d", "e","f", "g", "h", "i", "j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
// Prompt
cout << "How long would you like your password to be?";
cin >> input;
// Loop
for(int i = 0; i < input; i++)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
srand((time_t)ts.tv_nsec);
int random = (rand() % 26 + 1);
lett = lettArray[random];
password = password + lett;
sleep_for(milliseconds(10));
cout << "." << endl;
if (random == 0)
break;
}
// Output
cout << password << endl;
return 0;
}
問題があるかどうかはわかりませんが、割り当ての+1をランダムに削除してください。あなたは0から25の範囲で1から26までの数字を必要としません。 –
@MathieuVanNevelこれはエントロピーを増やそうとしていると思います。(これはここではあまり得られません...) – chtz
実際には、 'srand'を再呼び出しします。これはそれらの1つではありません – user4581301