Question as to why something works :P

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
#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.
cin >> choice;

This line gets input from the user (from cin) into the variable "choice".

More info: http://www.cplusplus.com/doc/tutorial/basic_io/#cin
Ok, let me break this down for you.

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.

Make sense?
Last edited on
Yes Paket..thanks again..that makes perfect since to me. So if i wanted them to type 'Y' instead of 1 would I do the following?:

1
2
case Y:
    cout << "You have selected Blacksmith!\n";


is that right?
Last edited on
On second thought that wouldn't work because choice is not type Char right?
Last edited on
case 'Y':
Here's an example for you if you wanted to check for characters instead.

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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <limits>
using std::numeric_limits;
using std::streamsize;

void pause();

int main()
{
    char choice;
    cout << "Enter Y for Yes or N for No: ";
    cin >> choice;

    switch(choice)
    {
        case 'y':
        case 'Y':
            cout << "You chose \"yes\"." << endl;
            break;
        case 'n':
        case 'N':
            cout << "You chose \"no\"." << endl;
            break;
        default:
            cout << "That is not a valid choice!" << endl;
            break;
    }
    
    pause();
    return 0;
}

void pause()
{
    cin.clear();
    cin.ignore(1, '\n');
    cout << "Press Enter to continue . . .";
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
Okay nice, I am starting to understand better. The code above did raise 2 questions:

#1. What does this line do: cin.clear();

#2. Can you have as many cases as you want in a switch statement?

Thanks for your help guys.
Last edited on
1: Clears out any error flags that may have been present the the time.

2: Yes*.

-Albatross
Last edited on
Okay last Question for a while I promise heh,

Using a switch statement like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch(choice)
    {
        case 'y':
        case 'Y':
            cout << "You chose \"yes\"." << endl;
            break;
        case 'n':
        case 'N':
            cout << "You chose \"no\"." << endl;
            break;
        default:
            cout << "That is not a valid choice!" << endl;
            break;
    }


If I wanted to make an If statement, if Y is chosen, do something....
How would I go about doing something like that?

PS: I am happy to see you guys help people, I will be sure to give something back to the community when I learn more. Thanks again :)
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.

-Albatross

P.S.- It's really no problem.
Last edited on
Keep asking 'em beginner questions, it's great for learning - and I'm too worthless myself for helping with other's advanced questions..
I'm too worthless myself for helping with other's advanced questions..
Don't you ever again talk about yourself like that!
Last edited on
Ok, here's another breakdown for you, Nikoru.

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch(choice)
    {
        case 'y':
        case 'Y':
            cout << "You chose \"yes\"." << endl;
            break;
        case 'n':
        case 'N':
            cout << "You chose \"no\"." << endl;
            break;
        default:
            cout << "That is not a valid choice!" << endl;
            break;
    }


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.
Last edited on
Topic archived. No new replies allowed.