How to check if the user is entering the same input twice?

I have a simple program that asks the user to select 3 players from a list. Is there any way to keep the user from selecting the same player twice? I tried creating an std::set to remember all the numbers and check for duplicates, but I'm pretty sure I implemented it wrong as I don't really know what std::sets are or how they even work.

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
62
63
64
  
#include <iostream>
#include <string>
#include <set>

int main()
{
	int m_NumOfPlayers;

	std::string m_PlayerSelection[6] =
	{
		"Miss Scarlet",
		"Mrs. Peacock",
		"Colonel Mustard",
		"Professor Plum",
		"Mrs. White",
		"Mr. Green"
	};

	std::string m_Number[6] =
	{
		"first",
		"second",
		"third",
		"fourth",
		"fifth",
		"sixth"
	};

	for (int i = 0; i < 3; i++)
	{
		std::cout << "\nPlease choose the " << m_Number[i] << " player" << ":" << std::endl << std::endl;
		for (int j = 0; j < 6; j++)
		{
			std::cout << (j + 1) << ". " << m_PlayerSelection[j] << std::endl;
		}

		int selection;
		std::cin >> selection;

		std::set<int> numbers;

		if (numbers.find(selection) != numbers.begin())
		{
			std::cout << "That selection has already been made" << std::endl;
			std::cin >> selection;
		}
		else
		{
			numbers.insert(selection);
		}

		while (selection < 1 || selection > 6)
		{
			std::cout << "Please choose a number between 1 and 6: ";
			std::cin >> selection;
		}
	}
	system("pause");
	return 0;
}


Last edited on
Here is slightly changed set solution, sttudy it and change according to your needs:
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
#include <iostream>
#include <string>
#include <set>

int main()
{
	std::string m_PlayerSelection[6] =	{
		"Miss Scarlet",
		"Mrs. Peacock",
		"Colonel Mustard",
		"Professor Plum",
		"Mrs. White",
		"Mr. Green"
	};
	std::string m_Number[6] = {
		"first", "second", "third", "fourth", "fifth", "sixth"
	};
	int players = 0;
	std::set<int> numbers; //You need set to stay persistent between iterations
	while (players < 3)	{
		std::cout << "\nPlease choose the " << m_Number[players] << " player:\n\n";
		for (int j = 0; j < 6; j++)
			std::cout << (j + 1) << ". " << m_PlayerSelection[j] << '\n';
		//We need to get proper number before checking it against set
		int selection;
		do {
			std::cout << "Please choose a number between 1 and 6: ";
			std::cin >> selection;
		} while (selection < 1 || selection > 6);

		if (numbers.find(selection) != numbers.end()){//find retirns end iterator if nothing was found
			std::cout << "That selection has already been made\n";
			continue; //Restart input again
		} else {
			numbers.insert(selection);
			++players;
		}
	}
}
Another solution is to remove already taken characters from choosing altogether.
That solution doesn't work because an error is thrown ("Please choose a number between 1 and 6") before the user has a chance to even input anything.
That solution doesn't work because an error is thrown ("Please choose a number between 1 and 6") before the user has a chance to even input anything.
It is not an error, but design choice. If you want, slightly redo input loop. Did you even try to work with it?
I modified your solution and it works perfectly now. Thank you for your help.
Last edited on
Topic archived. No new replies allowed.