#include<iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int choice;
cout << "\t Your adventure starts in a small town called Grokas Ferry!\n";
cout << "\n You were raised as a: \n";
cout << "\n 1. Blacksmith";
cout << "\n 2. Tailor";
cout << "\n 3. Warrior";
cin >> choice;
switch (choice)
{
case 1:
cout << "You have selected Blacksmith!\n";
break;
case 2:
cout << "You have selected Tailor!\n";
break;
case 3:
cout << "You have selected Warrior!\n";
break;
default:
cout << "Invalid Choice!\n";
}
cin >> choice;
return 0;
}
Okay I got this to do what I need it to do (thanks to packetpirate) However, I want to know how does it know that when 1 is pressed type backsmith. What part of the code is telling it to do that? Thanks for the help as always.
First, you have a variable called 'choice' which will store whatever input the user enters (such as 1, 2, or 3). Then, you have a switch statement. Switch statements are just like IF statements. It's saying that IF the case is that they enter the digit 1, it will perform the actions between the "case 1:" line and the next "IF" statement in the switch function. Break just shows the end of the "IF" statement.
You mean, tell the difference between 'y' and 'Y', but leave the structure of the switch statement intact and unchanged? Just an extra if statement in the section that outputs
You chose "yes".
You can input almost anything if not everything that is valid within a function inside a case: break; block.
First of all, putting multiple case statements together like I did in the above example is like saying "IF this OR IF that THEN do this". So for example:
IF the user input is Y OR y, then print "You chose yes."
IF the user input is N or n, then print "You chose no."
IF the user input isn't equal to any of the above statements, then do the default action, which is to print "That is not a valid choice!". Does that make any sense?
The break statement is used to prematurely end a switch statement. If the break statement didn't come in those statements above, it would continue in the switch statement to check if it was equal to any of the other cases. Not including break after you take action in a specific case can cause all sorts of errors.