Switch not working

So this is a part of my code. When I press '1' it should enter the case 1: in the switch, but it doesnt.. I can press 2 and 3. How come, what's going wrong?

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

bool closeGame = false;

int main(int argc, char** argv)
{
	displayMenu();
	srand(time(NULL));
		
	return 0;
}

void displayMenu()
{
	int choice = 0;

	std::cout << "1. Play\n";
	std::cout << "2. Add Word\n";
	std::cout << "3. Quit\n";
	std::cin >> choice;

	//Play the game function.
	std::cin >> choice;
	while(!closeGame)
	{
		
		switch(choice)
		{
		case 1:
				playGame();
				break;
			
		case 2:
		{
				system("cls");
				addWord();
				break;
			
		}
		case 3:
		{	
				std::cout << "Thank you and come again!";
				closeGame = true;
				break;
		}
				
			
		default:
			std::cout << "Invalid input.";
		

		}
	}

	

}


Here is the code for playGame().
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
65
66
67
68
69
70
71
72
73
74

void playGame()
{
	bool runGame = true;
	int guesses = 10;
	int guessCount = 0;
	int position;
	int wrongGuesses = 0;

	std::string word = randomWord();
	std::string correctWord;
	std::string guessed;	
	std::string guess;
	
	for (int i = 0; i < word.length(); i++)
	{
		correctWord.insert(i, "_");
	}

	while (runGame)
	{
		std::cout << "Welcome to hangman! Don't just hang there, go guess!\n\n\n";
		std::cout << "GUESSES LEFT: " << guesses << std::endl;
		std::cout << "Pleae guess a letter or full word\n\n";
		std::cout << "Letters you have guessed already: " << guessed << std::endl;
		std::cout << std::endl << correctWord << std::endl << std::endl << "Next letter or full word: ";
		std::cin >> guess;
		
		guessed.insert(guessCount,guess);
		guessCount++;
		position = word.find(guess);

		if (word == guess)//om man gissar rätt ord genom att skriva hela
		{
			std::cout << "Correct guess, you won!\n";
			runGame = false;
		}
		else

			if (word.find(guess) != std::string::npos)//om gissningen finns med i det rätta ordet
			{

				correctWord[position] = word[position];
				for (int j = 0; j < word.length(); j++)//går igenom hela ordet med gissningen och ändrar om man gissat något rätt
				{
					if(guess[0] == word[j])
					{
						correctWord[j] = guess[0];
					}
				}

				system("cls");

				if (correctWord == word)
				{
					std::cout << "Correct word, you won.\n";
					runGame = false;
				}
			}
			else
			{
				guesses-=1;
				wrongGuesses++;

				system("cls");

			}

			if (guesses == 0) //om man får slut på chanser
			{
				runGame = false;
			}
	}
}
You're missing some braces {}

Line 31 needs a } to close case 1:

Line 48 needs a { to open default:

To be a little poetic:
You had matching braces,
just in the wrong places

B-)
Last edited on
Cheers, also saw that I had 2 of cin >> choice, that's why I thought that the game didn't start.. had to press 1 twice ^^
Topic archived. No new replies allowed.