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 :)