Looping Switches

Hello, I am relatively new to C++ and I have read multiple guides / tutorials so I figured I would have a good enough understanding of the language to make a simple (really simple, I plan to expand on it later) game.
My problem is that it all works fine, pressing 1 will fight and pressing 2 will flee, but my problem comes when I enter something other than 1 or 2.
I tried using while, but it broke the program so it didn't do anything when I put 1 or 2 in.
I want it to start the program again when I enter something other than 1 or 2, so if I enter 3 for example, I want it to go back to the beginning so I can easily enter 1 or 2 without having to restart the program.

Here is my 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
#include <iostream>

main ()
{
    short Action;
    std::cout << "A monster appeared!" << std::endl;
    std::cout << "What would you like to do?" << std::endl;
    std::cout << "Press 1 to fight, and press 2 to run away." << std::endl;
    std::cin  >> Action;
    
    switch (Action)
    {
     case 1:
      std::cout << "You chose to fight! Good Luck!" << std::endl;
      std::cout << "You won!" << std::endl;
     break;

     case 2:
      std::cout << "You chose to run away!" << std::endl;
     break;

     default:
      std::cout << "Invalid action." << std::endl;
      std::cout << "Press 1 to fight, and press 2 to run away." << std::endl;
     break;    
    }
}


As you can see, it is simple, and I would certainly appreciate all the help that you can give me.
Thanks in advance :)
There are many ways to do this. For example
1
2
3
4
5
6
7
8
9
10
11
12
bool err;//error flag
do{
   err = false;
   cout << "1 or 2?\n";
   cin >> Action;
   switch(Action){
      ...
      default:
         cout << "oh no!\n";
         err = true;
   }
}while(err);


edit: forgot input..
Last edited on
Sadly, I am still struggling. Here is my full loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    do
    {
     err = false;
     switch (Action)
     {
      case 1:
       std::cout << "You chose to fight! Good Luck!" << std::endl;
       std::cout << "You won!" << std::endl;
      break;

      case 2:
       std::cout << "You chose to run away!" << std::endl;
      break;

      default:
       std::cout << "Invalid action." << std::endl;
       std::cout << "Press 1 to fight, and press 2 to run away." << std::endl;
       err = true;
      break;
      }
    }
    while (err);


However, it works fine until it tries to do the default case, then it just gets stuck in a loop and continues to post the lines
1
2
std::cout << "Invalid action." << std::endl;
std::cout << "Press 1 to fight, and press 2 to run away." << std::endl;

over and over again, without asking for my input.
It is possible that you input a invalid input like "5a2138$", this will set the error flag on the standard input. You need to set it back to normal.
http://www.cplusplus.com/reference/iostream/istream/
http://www.cplusplus.com/forum/articles/28558/
You have to move cin >> Action inside the loop.
Thank you very much for all your helpful answers guys :)
Topic archived. No new replies allowed.