Switch Case wit Chars?

Hello, I am currently learning about switch cases, and I do not understand if it is legal for it to switch commands for a Input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
switch (inPut)
{
  case 1:
     cout << "Matt"
          << "\n"
          << endl;
     break;
     
  case 2:
     cout << "19"
          << "\n"
          << endl;
     break;
     
  default:
    cout << sBrack;
    cin >> inPut;
     
     
}


For instance, if I input "Matt", this it displays what ever I like, but if I insert 19, it inserts something else... Any comments?
I don't really understand. Is inPut int, char or whatever?

Basically your saying - if inPut is equal to '1', do what case 1. If it's equal to '2', then it will do case 2. Then for everything else (default), it will do case default, which means everything other than 1 or 2. So if you enter 3, 19, etc, it will do default.
You can use the switch cases to do anything you want. However the input argument can only be either of type int or char. And the switch will only evaluate the selection once, so it wouldn't be of any use to you either. Unless you contain it within a loop of some kind.

Normally I only use the switch for a menu in the main program. Here is an example how I normally uses it, hope it help.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>

using namespace std; 

int main(){
char input;  
do
{	
	cout << "Enter: 	a for adding new node." << endl; 
	cout << "	d for deleting existing node by ssn." << endl; 
	cout << "	y for deleting ith node." << endl; 
	cout << "	f for finding node by ssn." << endl; 
	cout << "	i for finding all node by given last name." << endl; 
	cout << "	x for initializing the list." << endl; 
	cout << "	p for printing the content of the whole list" << endl; 
	cout << "	r for deleting all node in the list" << endl; 
	cout << "	q to quit" << endl; 
	cout << "Follow by return key in each case." << endl; 
	cout << "Enter: "; 
	cin >> input; 
	system("cls"); //on some system it's clear not cls. 
switch(input)
	{
	case 'A':
	case 'a':
		cout << "Adding new node." << endl;
		//NodeControlla.AddNode();
		break; 
	case 'D':
	case 'd':
		cout << "Deleting Node By SSN." << endl;
		//NodeControlla.DeleteNodeBySSN(); 
		break;
	case 'Y':
	case 'y':
		cout << "Deleting ith node." << endl; 
		//NodeControlla.DeleteNodeAtPos(); 
		break;
	case 'F':
	case 'f': 
		cout << "Finding node by ssn." << endl;
		//NodeControlla.FindNodeBySSN(); 	
		break;
	case 'I':
	case 'i': 
		cout << "Finding node by last name." << endl; 
		//NodeControlla.FindNodeByLName();
		break;
	case 'X':
	case 'x': 
		cout << "Initializing List." << endl; 
		//NodeControlla.InitializeList(); 
		break; 
	case 'P':
	case 'p': 
		cout << "Printing List." << endl; 
		//NodeControlla.PrintList(); 
		break;
	case 'R':
	case 'r': 
		cout << "Deleting all nodes in list." << endl; 
		//NodeControlla.DeleteAllNode(); 
		break; 
	case 'Q':
	case 'q':
	break; 
	default:
		cout << "Invalid Selection. Please try Again." << endl; 
	}//switch
cout << endl << endl; 
}while(!((input == 'Q')||(input == 'q')));
}//main
Last edited on
inPut was suppose to be my char, I was aiming for a user input, and a choice.. But thanks a lot! This really helped me, I think
Topic archived. No new replies allowed.