Ingo
Ingo

Reputation: 11

How to insert character into set?

I have a small game which starts with a menu and the player can choose between 6 options. The decision from the player will be written with a cin into a char variable. This works fine, and the game starts with the option. But the game also starts when the player enter another value and then the game has errors. To fix it, I wanted to use a character set with the possible options. But on every way I try to insert values into the set variable I get a compiler error.

When I try to initialize set while declaring I get "'a' was not declared in this scope":

set <char> Options {a,b};

If I try it like this, "no matching function for call to 'std::set::insert(const char [2])'|"

set <char> Options {"a","b"};

When I do it like this, I get also "'a' was not declared in this scope"

set <char> Options;

int main()

Options.insert(a);

I also tried this, but then again I get "no matching function for call to 'std::set::insert(const char [2])'"

set <char> Options;

int main()

Options.insert("a");

So now I'm very confused. With integer I have no problems, but when I try to use it with characters, I didn't get it working.

Can somebody help?

(This is my first question here, I hope it's well formulated)

Upvotes: 0

Views: 2707

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477060

Character literals are spelled with apostrophes:

set<char> Options {'a', 'b'};
//                 ^^^  ^^^

Upvotes: 6

Related Questions