vendredi 7 juillet 2017

C++ Making a random number/letter generator, something wrong in the code

I'm trying to make a word-list generator that generates random letters and numbers, however I'm having an error where it only gets the first word in the list right, the rest only displays 2 letters/numbers, there is no error in the compiler, at this point it feels like I've tried everything without any luck. Here is the code:

    #include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <windows.h>
#include <ctime>
using namespace std;

static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";  // chars we need for generation

int stringLength = sizeof(alphanum) - 1;

char genRandom(){
    return alphanum[rand() % stringLength];
}

int main(){


// WORD LISTS LENGTH
int WordListTotalLength;
int WordListThisLength = 0;
cout << " How many words should be added?" << endl;
cin >> WordListTotalLength;

// WORD LISTS LENGTH ENDS HERE

    SetConsoleTitle("LetterNumberGenerator");

    srand(time(0));
    std::string Str;

    int length = 0;

    cout << "Enter the length of each word:" << endl;
    cin >> length;
    length = length - 1;


    for(unsigned int i = 0; i < length; ++i){
    Str += genRandom();
    }
    ofstream myfile;
  myfile.open("OutputWordlist.txt");

  while (WordListThisLength < WordListTotalLength) {

  myfile <<Str + genRandom();
  myfile << "\n" ;
  genRandom();
     Str = genRandom();
  WordListThisLength = WordListThisLength +1;
  }
  myfile.close();
    cout << "Generation is finished." << endl;



cout << endl;
system("PAUSE");
return 0;
}

But all I get from the "OutputWordlist.txt" is the following:

If inputing that I want 5 words in total, with 10 letters or numbers on each line I end up with this:

N5VKP15QAW
EN
YK
48
OK

And sorry if my post is a bit blurry, this is my first time posting. Any help or suggestions would be very appreciated!




Aucun commentaire:

Enregistrer un commentaire