password hint

Apr 5, 2021 at 2:25pm
Hey guys im wrting a code but ran into a problem, i am asking the user to enter the password that is random from an array. For example if the hint of the password is bark the the password is dog and if the hint is meow then the password is cat etc.



i want to use arrays and random function but cant find out how to do it.
1
2
3
4
5
6

string password [] = {cat dog cow ... etc]
string hint[] = {meow bark moo.....etc]  

// please help thanks in advanced //
Apr 5, 2021 at 2:55pm
Create a structure
1
2
3
4
struct passHint {
  string password;
  string hint;
};


Then you have an array of them
1
2
3
4
passHint data[] = {
  { "cat", "meow" },
  { "dog", "bark" },
};


A password is at some data[i].password, and the hint will be at data[i].hint.


Apr 5, 2021 at 5:00pm
ok thanks for that i was wondering how to do it but still a little confused but gonna try that approach need to change a few things on my code thanks a lot ill post it in a lil bit
Apr 6, 2021 at 9:02am
What do you really want to do? Are you giving the user a random hint and then checking that the reply is correct?

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
#include <iostream>
#include <string>
#include <random>
#include <chrono>
#include <iterator>

struct passHint {
	std::string password;
	std::string hint;
};

auto rng {[]() {
	std::random_device seeder;

	return std::mt19937(static_cast<std::mt19937::result_type>(seeder.entropy() ? seeder() : std::chrono::system_clock::now().time_since_epoch().count()));
} ()};

int main()
{
	const passHint data[] = {
	  { "cat", "meow" },
	  { "dog", "bark" },
	};

	const std::uniform_int_distribution<size_t> distrib(1, std::size(data));
	const auto hnt {distrib(rng) - 1};
	std::string pass;

	std::cout << "Given a hint of " << data[hnt].hint << ". What is the password? ";
	std::getline(std::cin, pass);

	if (pass == data[hnt].password)
		std::cout << "Correct!\n";
	else
		std::cout << "Incorrect\n";
}


Apr 6, 2021 at 1:29pm
Can't you make the user supply a hint when they set the password and just give them back the same string without even using an array?
Apr 6, 2021 at 3:46pm
yes but i wanted to compare 2 arrays with the same that way i just add it to the array without making any changes in the code besides the arrays or is there a simpler way besides that
Topic archived. No new replies allowed.