RDS(Random Drug Screen) Random Name Picker

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.
Have you considered writing the program yourself instead of relying on some random code you found on the internet? Then maybe you might better understand why something is not working as you expect.

Ignoring the random code snippets:

I figured I would use a vector to automatically see how many names or lines are in the text file.

This seems like a decent approach. What exactly does this input file contain?

There are some people who need to be screened more than once.

So how do you know what people need to be screened more than once?

So their names have a probability of being chosen greater than others

This is related to the above question but how much higher of a probability should they have?

After they are chosen they are removed from the list but added back after a 30 day period.

This is fairly ambiguous, how are they to be removed from the list? Who is going to add them back after a 30 day period? Who is keeping track of this 30 day period? Should people that need to be screened more than once even be removed from the list?


The only way the 2nd example can go out of bounds (segment fault), is if the file is completely empty. But in that case the vector is not going out, its the uniform distribution. It is unfortunate that you are not error checking the file.
I would love to write it myself which was the plan, however I know very basic C++ (A Calculator in console that can insult you.) Input file is simply a First and Last Name, the next line would be the second name. I have in physical form, the list of names i am to enter into the list. They are color coded and the ones who are "Yellow" on the list are two be screened twice that month. I was thinking that they would be removed from the list and put into a secondary file. Have the application check the date to see if 30 days had passed and re enter them into the list. From what I can tell, everyone needs to be screened at least once, but their names randomly chosen throughout the month. Certain people need to be randomed twice throughout the month, same rules. Once again I am pretty much spending the day educating myself on how to do this. But I also know if I want something done right, I gotta come here.

Ill update you with a more "myself" code when i get it there. Thank you!

poteto I apologize, I will research error checking as well.
Last edited on
I have in physical form, the list of names i am to enter into the list. They are color coded and the ones who are "Yellow" on the list are two be screened twice that month.

So how is the computer supposed to know how individuals are to be screened? How are you going to modify this manual system to something that can be automated? Do the "Yellow" people remain constant? This part of the "exercise" seems to need further thought if you want to automate this task.

Have the application check the date to see if 30 days had passed and re enter them into the list.

Again you need to think of how the computer can do this. Do you plan to have this program run every day so that it can check your "list".

To me you really need to make sure you understand the requirements so that you can then design the program to match those requirements. And remember that design need to be accomplished before you even start to write a single line of code. It seems to me that you have several areas that you should have clarified. Otherwise it would probably just be easier to put the names in a hat and pick the number of names you need manually to fulfill the requirements.

Hey this is good.

So I was thinking everytime the Application was launched it would check the date, to see if it needs to put names back, Have a last run date, check and see how many days have passed, if thirty, re dump names. Good question on the yellows. Can we make an accompany array that details how many times they are to be screened and if they already had been? So that it will check does require screening more than once? No? Remove and put in the reserve to be added in a month Does it require another screening? then keep in the list. The application will more than likely be ran daily. As far as I can tell there are 47 names, so it would need to pull around 2-3 names a day. I know your almost scolding me, but this is good. Poteto I need to basically just make sure it's not an empy file and throw a message if it is asking them to close and reopen, something along those lines? I'll google up on that as well.

So a greenNames.txt for 1 screens and yellowNames.txt for 2 screens.
Last edited on
Topic archived. No new replies allowed.