So I was asked at work if I could come up with a way to pick a random name from a list for Random Screening at where I work.
The guidelines are like this. The names are in a text file, so Names.txt, I figured this can be a console application with the txt file beside it in a folder.
I figured I would use a vector to automatically see how many names or lines are in the text file. There are some people who need to be screened more than once. So their names have a probability of being chosen greater than others, So I need to add a function to include that.
After they are chosen they are removed from the list but added back after a 30 day period.
I've been scouring the forums but I'm afraid I don't know where to start I used desoxena's code at first which ran but gave me no output. Figured it would be a good starting point. But having the txt document in the same folder as the main.cpp file seems to have no effect, that or I'm getting a compiler error that doesn't show up in debug.
Desoxena's modified code
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
|
#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <string>
#include <fstream>
#include <time.h>
using namespace std;
int main()
{
std::cout << "------------------------------Random Drug Screen (RDS) Written by Patrick Ward for LLCHC--------------------------------\n\n";
std::cout << " This Program will choose a name randomly in the Name.txt file\n\n";
std::cout << " Higher chances can be set per name.\n\n";
std::cout << "------------------------------------------------------------------------------------------------------------------------\n\n";
ifstream nameBank;
nameBank.open("Names.txt");
string names[100]; //holds the names, currently 100
string randomNames[2]; //Holds the names after their generated from names.txt
int nameArray = 0;
int randomNumber;
while (nameBank.good()) {
getline(nameBank, names[nameArray]);// reads the names from names.txt and puts the namearray in names[]
nameArray++;
}
for (int iteration = 0; iteration < 2; iteration++) { //makes the program iterate 2 times, giving you 2 random names
srand(time(NULL));//using time to generate a random number to pick a name with
randomNumber = rand() % 100 + 1; //creates a random number between 1 and 100
randomNames[iteration] = names[randomNumber];
}
for (int iteration = 0; iteration < 2; iteration++) {
cout << randomNames[iteration] << endl; //outputs 2 names at once
}
nameBank.close();
system("pause");
}
|
I also tried a modified Toby Spreight code from stack Exchange
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
|
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <vector>
std::vector<std::string> read_names(const char* filename)
{
std::ifstream in{ filename };
std::vector<std::string> names;
std::string name;
while (getline(in, name))
if (!name.empty())
names.push_back(std::move(name));
return names;
}
int main()
{
const auto first_names = read_names("Names.txt");
std::mt19937 rng{ std::random_device{}() };
std::uniform_int_distribution<size_t> first_dist{ 0, first_names.size() - 1 };
for (int i = 0; i < 100; ++i) {
int first = first_dist(rng);
std::cout << first_names[first] << '\n';
}
}
|
Which gave me a vector out of bounds error.
I'm going to continue doing research on how to properly and correctly accomplish this to make it as useful and user friendly as possible. Hopefully you guys can help me out on how to do this the right way. Regardless I'm having fun spending my day doing this.