2016-12-09 4 views
1

私は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; 
} 
+2

問題があるかどうかはわかりませんが、割り当ての+1をランダムに削除してください。あなたは0から25の範囲で1から26までの数字を必要としません。 –

+2

@MathieuVanNevelこれはエントロピーを増やそうとしていると思います。(これはここではあまり得られません...) – chtz

+0

実際には、 'srand'を再呼び出しします。これはそれらの1つではありません – user4581301

答えて

0

は、このプログラムをランダムに失敗した理由は、(しゃれが意図した)あなたはC-配列を使用していることで、あなたは間違ったインデックスの範囲を得ました。

lettArrayなどのC配列は、配列の境界に違反していないかどうかをチェックしません。あなたの例では、このプログラムを実行すると、そのメモリアドレスにstring要素がないため、lettArray[26]はセグメンテーションフォールトを与えます。 C配列は境界をチェックしないので、何が問題になったのかを知ることは難しいかもしれません。これは複雑なプログラムでは特に問題になることがあります。なぜなら、メモリアドレスに何かがあると、無意味な結果が得られるからです。

より良い実装がstd::vectorを使用することになります。

#include <iostream> 
#include <cstdlib> 
#include <chrono> 
#include <thread> 
#include <time.h> 
#include <vector> 
using namespace std; 
int main() 
{ 
    // You don't need these two lines 
    //using namespace std::this_thread; 
    //using namespace std::chrono; 

    // Vars 
    string lett; 
    int input; 
    string password(""); 

    // Vector index range: 0-25 
    vector<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"}; 

    // You only need to initialise the seed once. 
    struct timespec ts; 
    clock_gettime(CLOCK_MONOTONIC, &ts); 
    srand((time_t)ts.tv_nsec); 

// Prompt 
    cout << "How long would you like your password to be?"; 
    cin >> input; 

// Loop 
    for(int i = 0; i < input; i++) 
    { 
     int random = (rand() % 26); // You get results between 0-25 
     // Using lettArray.at(26) will result in a crash with a clear 
     // message that bounds are violated 
     lett = lettArray.at(random); 
     password = password + lett; 
     cout << "." << endl; 

     // Don't see a reason for this if statement 
     // if (random == 0){ 
     //  continue; 
     //} 
    } 
// Output 
    cout << password << endl; 
    return 0; 
} 

また、私は(あなたは一度だけこれを必要とする)は、ループの外にランダムシードを移動し、10ミリ秒の一時停止を持ってする理由もありません。あなたはあなたの結果が無作為に無作為に選ばれるのを見るでしょう。

+0

ありがとう! :)、私はC++を初め、Javaから来ています。 –

+0

あなたは歓迎です:) – nikaza

関連する問題