Assigning Random Number Generator
Nov 23, 2016 at 11:27pm UTC
Hello I am trying to make a program that takes the first and last names of people and makes a username for each person, the username is the first letter of the first name and 7 letters of their last name. I believe I did this correctly..
I then want to take a txt file of passwords and assign it to an array (which again I believe I did correctly) Then I want to assign each username one of the passwords using a random number generator. I have 50 usernames and passwords so I made a random number generator that makes 50 random numbers, my problem I cant seem to be able to use my random number generator to assign each username one of the passwords and not repeat passwords. Any help would be greatly appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include<iostream>
#include<fstream>
#include<string>
int i = 0;
int main()
{
std::string firstName; // Stores the first name of the user
std::string lastName; // Stores the last name of the user
std::string Users[50];
std::string passwords[50]; //
std::ifstream inputHandler;
std::ofstream outputHandler;
inputHandler.open("names.txt" );
outputHandler.open("userNames.txt" );
inputHandler >> firstName >> lastName;
while (!inputHandler.eof())
{
outputHandler << firstName[0] + lastName.substr(0, 7) + " " ;
inputHandler >> firstName >> lastName;
}
outputHandler.close();
inputHandler.close();
std::ifstream file("userNames.txt" );
if (file.is_open())
{
std::string Users[50];
for (int i = 0; i < 50; ++i)
{
file >> Users[i];
std::cout << Users[i] << std::endl;
}
}
std::ifstream fileX("passwords.txt" );
if (fileX.is_open())
{
std::string passwords[50];
for (int i = 0; i < 50; ++i)
{
fileX >> passwords[i];
std::cout << passwords[i] << std::endl;
}
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream>
#include <time.h> //needed to use randomization
int main()
{
int randomNumber;
srand(time(NULL));
for (int x = 0; x < 50; x++)
{
randomNumber = rand() % 50;
std::cout << "Random number " << x + 1 << " " << randomNumber << std::endl;
}
return 0;
}
Nov 24, 2016 at 12:47am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <string>
#include <random>
#include <ctime>
#include <algorithm> // std::shuffle
int main()
{
constexpr std::size_t N = 5 ; // 50
std::string user_name[N] = { "Twentyone" , "Twentytwo" , "Twentythree" , "Twentyfour" , "Twentyfive" } ;
std::string password[N] = { "one" , "two" , "three" , "four" , "five" } ;
// http://en.cppreference.com/w/cpp/algorithm/random_shuffle
std::shuffle( password, password+N, std::mt19937( std::time(nullptr ) ) ) ;
for ( std::size_t i = 0 ; i < N ; ++i ) std::cout << user_name[i] << " : " << password[i] << '\n' ;
}
http://rextester.com/SYBA54656
Nov 24, 2016 at 1:10am UTC
Thanks for the reply!! However when I attempted to use it in my code it simply outputted : 50 times, I am not sure what I did wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include<iostream>
#include<fstream>
#include<string>
#include<random>
#include<ctime>
#include <algorithm>
int i = 0;
int main()
{
std::string firstName; // Stores the first name of the user
std::string lastName; // Stores the last name of the user
std::string Users[50];
std::string passwords[50]; //
std::ifstream inputHandler;
std::ofstream outputHandler;
inputHandler.open("names.txt" );
outputHandler.open("userNames.txt" );
inputHandler >> firstName >> lastName;
while (!inputHandler.eof())
{
outputHandler << firstName[0] + lastName.substr(0, 7) + " " ;
inputHandler >> firstName >> lastName;
}
outputHandler.close();
inputHandler.close();
std::ifstream file("userNames.txt" );
if (file.is_open())
{
std::string Users[50];
for (int i = 0; i < 50; ++i)
{
file >> Users[i];
std::cout << Users[i] << std::endl;
}
}
std::ifstream fileX("passwords.txt" );
if (fileX.is_open())
{
std::string passwords[50];
for (int i = 0; i < 50; ++i)
{
fileX >> passwords[i];
std::cout << passwords[i] << std::endl;
}
}
constexpr std::size_t N = 50;
std::shuffle(passwords, passwords + N, std::mt19937(std::time(nullptr ) ));
for (std::size_t i = 0; i < N; ++i) std::cout << Users[i] << " : " << passwords[i] << '\n' ;
return 0;
}
Nov 24, 2016 at 1:15am UTC
I took the cout out of line 40 and 50 thinking that those might be messing it up but its still not outputting them. I dont understand why it outputs them in lines 40 and 50 but not when i write the same thing with the random.
Nov 24, 2016 at 2:44am UTC
You need to read the user names and passwords into the arrays declared on lines 13 and 14.
Haven't really looked at the code, but you need to comment out (or delete) lines 36 and 46.
Topic archived. No new replies allowed.