-4

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!

ytobi
  • 535
  • 1
  • 9
  • 19
Cryptic5
  • 11
  • 2
  • I only have two links for you: http://en.cppreference.com/w/cpp/numeric/random , https://channel9.msdn.com/Events//2013/rand-Considered-Harmful , well three: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Jesper Juhl Jul 07 '17 at 19:00
  • 4
    Use a debugger an step through your code. –  Jul 07 '17 at 19:02
  • I bet, I am extremely new to programming so its prolly looks like crap too! I will check out the links Jesper thank you. – Cryptic5 Jul 07 '17 at 19:03
  • 1
    Please make your code presentable, at a minimum by indenting it correctly and consistently. You *and others* will read your code many more times than you write it. http://format.krzaq.cc/ – StoneThrow Jul 07 '17 at 19:15
  • The code is a bit messy but I understand. It's put it into stackoverflow. Don't be discouraged all, you'll be fine. When asking questions in the future, try to paste snippets. Like for example, I would've just posted the while loop and asked why that didn't work, rather than the whole program. Keep on coding, you're doing fine! – Chad K Jul 07 '17 at 20:40

1 Answers1

1

I took a look at your code and refactored it quite a bit. Here is the repl.it of it https://repl.it/JS45/0

I was too confused as to what your code did to actually look at what was wrong, but I did sort of use the style you were going for.

I changed the GenRandom function to GenRandomWord(length), and instead of using a while loop, I used a for loop which is easier to see how long it will run for

Also I just printed out the characters instead of saving it to a file. I'm sure you can figure out how to do that

Chad K
  • 832
  • 1
  • 8
  • 19