I'm relatively new to this site (and programming in general), so if this post is horrible like the last one, let me know if there's something I should change about it, and I'll try to do that.
So I'm given an array of 50 passwords in array Passwords, and my immediate challenge I'm facing is having to randomize their arrangement, with the help of a random number generator. For some reason, with the code I have, I'm making some mistake somewhere, resulting in the code being stuck in a loop or something. I know I'm close to being correct, but I just don't know where I made a mistake.
I tried to create a random number, check to see if it's already stated in array PasswordsTemp, and then: (if it is already in the array, do nothing), (if it isn't already in the loop, add it to the loop at the next available index). In order to then select another random number, this code has to loop. I set it to loop 600 times to (practically) ensure that it guesses every number 0 to 50 at some point. Following this, I just coded a simple cout for me to see what the array looks like.
Any help/advice would be appreciated. Just be aware that I'm relatively new here, so maybe try to go a little easy on me, or maybe just help someone else in lieu of berating me. Thanks.
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
int main()
{
std::string firstname = "";
std::string lastname = "";
std::string lastnametemp = "";
std::string fullname = "";
std::string Users[50] = {""};
std::string Passwords[50] = {""};
std::string NewPass = {""};
std::string temp = {""};
int PasswordsTemp[50] = {60};
int randomNumber = 0;
bool repeat = false;
int A = 0;
int i = 0;
std::ifstream inputHandler;
inputHandler.open("names.txt");
while (!inputHandler.eof())//ignore this part. I know it works.
{
inputHandler >> firstname >> lastname; //reads first name and last name of one line
lastnametemp = lastname.substr(0, 7); // sets temp last name as being at most, 7 letters long
fullname = firstname[0] + lastnametemp; // combines the two into a full name
Users[i] = fullname; //creates Users array
i = i + 1;//indexes data
}
std::ifstream indata;
indata.open("passwords.txt");
i = 0;
while (!indata.eof())
{
indata >> Passwords[i];//reads in passwords into array called Passwords
i = i + 1;
}
inputHandler.close();
//*******************************************************
//****************Confusion hereafter********************
//*******************************************************
for (i = 0; i < 600; i++)//Loops a long time to (practically) ensure that all numbers 0 to 50 are guessed atleast once
{
srand(time(NULL));
randomNumber = rand() % 51;//Produces random number between 0 and 50
for (i = 0; i < 51; i++)
{
if (randomNumber == PasswordsTemp[i])
{
repeat = true;//determines if the random number appears anywhere in the PasswordsTemp array; repeat = true if it appears anywhere
}
}
if (repeat == false)//if the random number doesn't appear anywhere, assign the random number an index in Passwordstemp
{
PasswordsTemp[A] = randomNumber;
A = A + 1;
}
}
//*****************************************************************************
//**************************End of Confusion***********************************
//*****************************************************************************
for (A = 0; A < 51; A++)//Checks to see if the passwords are randomized
{
temp = Passwords[PasswordsTemp[A]];
std::cout << temp << std::endl;
}
return 0;
}