Hesham Mahmoud - 7 months ago 17

C++ Question

I have a switch case that is looping on the length of different strings:

meaning

`switch(x.GetLength())`

{

case 0 // means string length is 0

... and so on

}

The problem i am facing is that there are two strings with the same length of 6, So i will have 2 cases with the same number

case 6

case 6

The string in the first case comes always in the form of numbers, does not contain any Letters/ characters

The string in the second case comes always with character P in the beginning of the string.

I want to deffrentiate between the two cases: what i tried is to switch over

(x.GetLength(), X.GetAt(0))

but it gives an error that the case expression is not constant

otherwise i cannot have two cases with the same number

`switch(m_strScanned.GetLength(), X.GetAt(0)) // switch over String Length`

{

case (6,2): // This case where the 6 digits are all numbers

WTN: if (m_strScanned.Mid(2,1).FindOneOf(".,/*-+ABCDEFGHIJKLMNOPQRSTUVWXYZ")>=0) goto Y;

if(m_strScanned.Mid(3,1).FindOneOf(".")>=0) goto X;

else

{

....

break;

}

case (6,'P'):

goto TYPE; // this is where the first digit is P

TYPE: if(m_strScanned.Mid(2,1).FindOneOf("ABCDEFGHIJKLMNOPQRSTUVWXYZ")>=0)

goto Y;

if(m_strScanned.Mid(3,1).FindOneOf(".")>=0) goto X;

else

{

....

break;

}

Is there a solution avoiding if else conditions

Thanks for the help

Answer

There is no solution using only a `switch`

statement and `case`

labels, you have to have one single `case`

and then use e.g. `if`

inside it to select the correct code:

```
switch (m_strScanned.GetLength())
{
...
case 6:
if (m_strScanned[0] == 'P')
{
// Handle this special case
}
else
{
// Handle this other special case
}
break;
...
}
```