#include <iostream>
int main()
{
int num1 = 0;
bool repeat_switch = true;
while(repeat_switch != false)
{
std::cout << "Enter an integer between 1 - 5." << std::endl;
std::cin >> num1;
switch(num1)
{
case 1:
std::cout << "You entered 1!" << std::endl;
repeat_switch = false;
break;
//..... case 2 - 5
default:
std::cout << "Invalid input, enter a number 1 - 5 again." << std::endl;
break;
}
//if the number isn't 1 - 5 then the condition for the loop (repeat_switch) will always be
//true and therefore the switch will keep repeating itself. If it is, it'll turn false and the
//while(...) will end
}
return(0);
}
You can edit the cin >> to filter out results (see: firedraco's link) , but I just did it quickly.
Thank you all for responding. Sorry I'm a little slow on returning. I'm making a Pick Your Own Adventure game, which is what I need the loop solution for. Anyone care to have a look at it so far?
Awesome. It will be nice to have some feedback. There is some bad language and some very dry humor, just warn you all. Sorry if the code is messy; again, self taught.
#include <iostream>
#include <stdlib.h>
#include <string>
usingnamespace std;
string name;/* The player's name.*/
int main(int argc, char *argv[])
{
int choice; /*The choice for each scenario.*/
cout << "Hey. Yes you. It's a black screen and you." << endl;
cout << "Who else would I possibly talking to?" << endl;
cout << "Anyway, what is your name? " ;
cin >> name;
cout << endl;/*Blank line. Need to find better method.*/
cout << name << " huh? " << endl;
cout << "Well let's get started " << name << "." << endl;
cout << endl;
/*Question 1*/
cout << "You are in a dimly lit room." << endl;
cout << "You look around a notice doors on either side of you." << endl;
cout << "Which one do you pick?" << endl;
cout << "1) Door on the left." << endl;
cout << "2) Door on the right." << endl;
cout << "Enter a choice: ";
cin >> choice;
cout << endl;
switch (choice) {
case 1:/*Correct choice to Question 1. Leads to Question 2.*/
cout << "The door leads to a hallway, with the same poor lighting." << endl;
cout << "You reach the end of the hallway and come to a door." << endl;
/*Question 2*/
cout << "You open the door, and on the other side is a room with a guard." << endl;
cout << "What do you do?" << endl;
cout << "1) Run in and beat him over the head." << endl;
cout << "2) Sneak up and snap his neck like a twig." << endl;
cout << "Enter a choice: ";
cin >> choice;
cout << endl;
switch (choice) {
case 1:/*Incorrect choice to Question 2.*/
cout << "The guard hears you running and screaming like an idiot..." << endl;
cout << "...He impales you. You died. Good job." << endl;
break;
case 2:/*Correct choice to Question 2. Leads to Question 3.*/
cout << "You sneak up quietly and say (insert cliche remark) before snapping his neck." << endl;
/*Question 3*/
cout << "You take the gaurd's sword. You have a feeling you'll need it." << endl;
cout << "You hear strange noises down a corridor at the end of the room." << endl;
cout << "At the other end of the room is a window you can climb out of and escape." << endl;
cout << "So, where do you go from here?" << endl;
cout << "1)Jump out the window. Screw this place. There's better adventures elsewhere." << endl;
cout << "2)Well any good adventurer investigates the noises right?" << endl;
cout << "You know how this works by now. ";
cin >> choice;
cout << endl;
switch (choice) {
case 1:/*Incorrect choice to Question 3.*/
cout << "You jump out the window and land right safely on the ground." << endl;
cout << "Unfortunately for you, there are six guards practicing their archery." << endl;
cout << "You get filled full of arrows. They try to bury you later but you're" << endl;
cout << "stuck to that tree something awful." << endl;
break;
case 2:/*Correct choice to Question 3. Leads to Question 4.*/
cout << "You head towards the corridor to see what's going on." << endl;
cout << "You walk for what feels like hours." << endl;
break;
default:
cout << "How about a choice that we can actually use, huh?" << endl;
break;
}
break;
default:
cout << "1 or 2 stupid." << endl;
break;
}
break;
case 2:/*Incorrect choice to Question 1.*/
cout << "The door leads to a well-lit feast hall." << endl;
cout << "You take one step into the room and...fall down a pit full of ravenous" << endl;
cout << "squirells." << endl;
cout << "Good job stupid. You killed yourself already." << endl;
cout << "Now you'll never get the cookies at the end." << endl;
break;
default:
cout << "Hey dumbass, You only have to type 1 or 2." << endl;
cout << "Is it really that damn hard?" << endl;
break;
}
system("PAUSE");
return 0;
}
1.) You could move name into main since you don't need it to be global.
2.) Check that article I mentioned to make sure your program doesn't break if someone enters invalid input
3.) Don't use system(), read here for more info: http://www.cplusplus.com/forum/articles/11153/
Thanks again firedraco. I have music composed too, but I'm saving that until the program is actually where I want it in terms of being done. Did you happen to find any grammatical/spelling errors? Any jokes to cheesy?
edit: screw, thanks for the link to that ebook. it was free, and it looks fantastic.