SRL Archive - C/C++ Help and Tutorials

How do I create Case statements in C++?


Recursive

2012-07-11 19:39:39

I want to create a function that does something similar to this:

int WhatToDo(char *Something)
{
case &Something of
{
1:
}

}

Basically, if user types word, return a value that represents word the person types in.

I think I might be leaning more towards pascal as I wrote that, but I have no idea how to do it in c++. I looked online and there was not a clear solution on how to accomplish the task. Does anyone know how to achieve something similar to that but in c++?

Frement

2012-07-11 19:45:30

switch(Something)
{
1: __asm { nop }
}

JJordan

2014-11-11 13:10:27

Basic


switch(condition) { // in this case the condition would involve an integer (matches case)
case 1:
// do something
break;

case 2:
// something else
break;

default:
//what do do if other cases aren't met
}


You can also


switch(condition) {
case 1: case 2: case 3: case 4:
// do something when one of those cases are true
break;

default:
//yeah stuff

}


Condition can be char, bool, int.
Probably something else but i'm hungover…

Lipcot

2014-11-11 13:15:08

From: JJordan
. . . . . .



akarigar

2014-11-11 14:39:37

In most languages, it is called a switch statement. Here's a pretty good mini tutorial. If you want any more details, don't hesitate to ask. Good luck.

The Spark

2014-11-11 16:40:26

Don't think people realize this thread is 2 years old………………….

Let the threads rest in peace guys

J_R

2014-11-12 22:19:06

In their defenses, most threads on this particular section are about a year or more old and I found JJordan's explanation easier to understand than the one given two years ago.