-2

I'm making an "Identity generator" where the computer randomly picks lines from .txt files.

Although it works fine with the first part of the code, when I repeat that code and change the variables it still uses the old txt file.

Expected result:

Full name: {random first name} {random sur name}
Address: {random address}

Actual result:

Full name: {random first name} {random first name}
Address: {random first name}

My code

    cout << "Full name: ";

    srand((unsigned) time(NULL)); 
    std::ifstream firstnamefile("englishfirstname.txt");
    int total_lines = 0;
    while(getline(firstnamefile,line))
    {
        total_lines++; 
        lines.push_back(line);  
    }
    int random_number = rand() % total_lines;
    cout << lines[random_number] << " ";

    //--- Surname ---

    srand((unsigned) time(NULL)); 
    std::ifstream surnamefile("englishsurname.txt");
    total_lines = 0;
    while(getline(surnamefile,line))
    {
        total_lines++; 
        lines.push_back(line);  
    }
    random_number = rand() % total_lines;
    cout << lines[random_number] << endl;

    // --- Address ---

    cout << "Address: ";
    
    srand((unsigned) time(NULL)); 
    std::ifstream addressfile("addresses.txt");
    total_lines = 0;
    while(getline(addressfile,line))
    {
        total_lines++; 
        lines.push_back(line);  
    }
    random_number = rand() % total_lines;
    cout << lines[random_number] << endl;

The .txt files are just a list of names for example:

John
Michael
Matthew
Etc...
user229044
  • 232,980
  • 40
  • 330
  • 338
Tetie
  • 365
  • 1
  • 14
  • 3
    OT: call `srand((unsigned) time(NULL))` only once at the start of the program, or better don't call it at all during the debugging process, then you'll most likely get the same sequence of pseudo random numbers on each run, which might simplify debugging. – Jabberwocky Oct 27 '22 at 14:14
  • Alright, I don't know a lot about those things so I just put them everywhere. – Tetie Oct 27 '22 at 14:16

1 Answers1

2

You never clear lines, so the start of the vector is always going to be your list of first names. You do reset total_lines to 0 before reading each file, so your random range is 0..total_lines each time, so you're always picking from the start of the array, which again, is all first names.

Assuming lines is a std::vector, you need lines.clear() along with setting total_lines to 0 to reset the state between each file read.

Note that you have three identical blobs of code that differ only by a file name; this is a very good candidate for a function that accepts a file as input, and returns a random line as output.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Thank you so much! I'm pretty new to c++ coding and particulary file reading. I tried resetting total_lines before but I really just forgot about `lines`. It works really well now. I'll also try to compact the code as you said. – Tetie Oct 27 '22 at 14:22
  • Or instead of clearing `lines` so you can reuse it, use different variables for `firstNames`, `surnames`, and `addresses` – Nathan Pierson Oct 27 '22 at 15:04