Dev C++ Switch
Sometimes when creating a C++ program, you run into a situation in which you want to compare one thing to a number of other things. Let's say, for example, that you took a character from the user and wanted to compare this to a number of characters to perform different actions. For now, let's just say that you have three commands which operate from the keys: 'h', 'e', and 'q'.
Attr (C11) - any number of attributes: condition - any expression of integral or enumeration type, or of a class type contextually implicitly convertible to an integral or enumeration type, or a declaration of a single non-array variable of such type with a brace-or-equals initializer. Init-statement (C17) - either. Switch case in C. By Alex Allain. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.
If c is a lowercase 'a', lowercasea is incremented and the break statement terminates the switch statement body. If c isn't an 'a' or 'A', the default statement is executed. Visual Studio 2017 and later: (available with /std:c17) The fallthrough attribute is specified in the C17 standard. You can use it in a switch statement.
- A list of the C tutorials available on Dev-HQ.
- Switch case statements are a substitute for long if statements that compare a variable to several integral values The switch statement is a multiway branch stat Switch Statement in C/C Switch case statements are a substitute for long if statements that compare a variable to several integral values.
Dev C++ Switch Menu
We can use an if-statement to check equality to 'h'
, 'e'
, and 'q'
- remember that we use single quotes because we're dealing with chars, not strings. What is little snitch uiagent. So let's just write the code in the way we're used to (and let's wrap a while loop around it so it keeps getting a character and acting upon what it was, because that makes our program slightly better and easier to test), using if-statements, like the following:
This program should be something you're comfortable with creating by this point, however your programmer instincts should also be kicking in and telling you that you shouldn't be repeating so much code here. One of the core principles of object orientated programming is DRY: Don't Repeat Yourself. In this program we're having to repeat a lot of code for the 'else if's including using ch
every time we're doing a comparison.
The solution to this 'problem' is what this tutorial is all about: switch statements. These are essentially just a really nice way to compare one expression to a bunch of different things. They are started via the switch
keyword, and from there comparisons are made using a case:
syntax. It isn't easily described in words, so take a look at the syntax below:
The different case
s essentially act as many if/else-ifs in a chain, and an 'else' type clause can be specified by using default
:
Dev C++ Switch Case Example
This type of functionality should be reasonably easy to understand with if-statements securely under your belt, and so if you're feeling brave - try porting the basic program we created at the start of this tutorial to use if-statements. How to auto tune voice in android. If you're not quite that brave, the cleaner and neater switch
version of the code is below:
Dev C++ Switch
void Result_Function(char x, int a, int b);
int t_to_d(string y);
int main()
{
int d1;
int d2;
string t1;
string t2;
char op;
vector<string>v(10);
v[0] = 'zero';
v[1] = 'one';
v[2] = 'two';
v[3] = 'three';
v[4] = 'four';
v[5] = 'five';
v[6] = 'six';
v[7] = 'seven';
v[8] = 'eight';
v[9] = 'nine';
cout << 'Please specify the type of inputs.' << endl;
cout << '(1) digit digit operation -- type 1 n';
cout << '(2) digit text operation -- type 2 n';
cout << '(3) text digit operation -- type 3 n';
cout << '(4) text text operation -- type 4 n';
int type;
cin >> type;
while (type != 1 && type != 2 && type != 3 && type != 4)
{
cout << 'Sorry! Please only choose to type the numbers from 1 to 4 to specify the kind of inputs you want. n';
cout << 'Please specify the type of inputs.' << endl;
cout << '(1) digit digit operation -- type 1 n';
cout << '(2) digit text operation -- type 2 n';
cout << '(3) text digit operation -- type 3 n';
cout << '(4) text text operation -- type 4 n';
cin >> type;
}
switch(type)
{
case 1:
cout << 'You chose digit digit operation. n';
cout << 'Now please enter the digits you want and the operation. n';
cin >> d1 >> d2 >> op;
break;
case 2:
cout << 'You chose the digit text operation. n';
cout << 'Now please enter the digit, text, operation. n';
cin >> d1 >> t2 >> op;
d2 = t_to_d(t2); // 'd2 DOES NOT ACCEPT THE VALUE GIVEN BY THE FUNCTION'
break;
case 3:
cout << 'You chose the text digit operation. n';
cout << 'Now please enter the text, digit, and the operation. n';
cin >> t1 >> d2 >> op;
d1 = t_to_d(t1); // 'd1 DOES NOT ACCEPT THE VALUE GIVEN BY THE FUNCTION'
break;
case 4:
cout << 'You chose the text text opeation. n';
cout << 'Now please enter the texts and operation. n';
cin >> t1 >> t2 >> op;
d1 = t_to_d(t1);
d2 = t_to_d(t2);
break;
}
Result_Function(op,d1,d2);
}
void Result_Function(char x, int a, int b)
{
switch(x)
{
case '+':
cout << 'The sum of ' << a << ' and ' << b << ' is: ' << a + b << endl;
break;
case '-':
cout << 'The subtraction of ' << a << ' and ' << b << ' is: ' << a - b << endl;
break;
case '*':
cout << 'The product of ' << a << ' and ' << b << ' is: ' << a*b << endl;
break;
case '/':
double q = a/b;
cout << 'The quotient of ' << a << ' and ' << b << ' is: ' << setprecision(20) << q << endl;
break;
}
}
int t_to_d(string y)
{
vector<string>v(10);
v[0] = 'zero';
v[1] = 'one';
v[2] = 'two';
v[3] = 'three';
v[4] = 'four';
v[5] = 'five';
v[6] = 'six';
v[7] = 'seven';
v[8] = 'eight';
v[9] = 'nine';
for( int i = 0; i < 10; i++)
{
if(v[i] y)
{
return i;
break;
}
}
}