0

I wrote a simple function as the following, but it does not work as expected, in C++, the if statement doesn't work in the block of switch

void any2ten(string origin, int type)
{
    if(! (type == 2 || type == 8 || type == 16))
    {
        cout << "unsupport this " << endl;
        return;
    }

    int result = 0;

   for (int index = 0; index < origin.length(); index++)
    {
        int tmp = 0;
        switch (origin[index])
        {
            if (type == 16) 
            {
            case 'F':
            case 'f':
                tmp = 15 * pow(type, index); break;
            case 'E':
            case 'e':
                tmp = 14 * pow(type, index); break;
            case 'D':
            case 'd':
                tmp = 13 * pow(type, index); break;
            case 'C':
            case 'c':
                tmp = 12 * pow(type, index); break;
            case 'B':
            case 'b':
                tmp = 11 * pow(type, index); break;
            case 'A':
            case 'a':
                tmp = 10 * pow(type, index); break;
            case '9':
                tmp = 9 * pow(type, index); break;
            case '8':
                tmp = 8 * pow(type, index); break;
        }
        if (type == 8 || type == 16) 
        {
            case '7':
                tmp = 7 * pow(type, index); break;
            case '6':
                tmp = 6 * pow(type, index); break;
            case '5':
                tmp = 5 * pow(type, index); break;
            case '4':
                tmp = 4 * pow(type, index); break;
            case '3':
                tmp = 3 * pow(type, index); break;
            case '2':
                tmp = 2 * pow(type, index); break;
        }
    case '1':
        tmp =  1 * pow(type, index); break;
    case '0':
        tmp =  0; break;

    default:
        cout << "wrong character has got" << endl;
        return;
        break;
    }
    result += tmp;
  }
  cout << result << endl;
}

while I test the function as any2ten("aa", 8), the result is 90 rather than wrong character.

is there anything wrong?

wanger
  • 145
  • 1
  • 6
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Mar 06 '17 at 08:09
  • 2
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Mar 06 '17 at 08:10
  • See also Duff's Device : http://stackoverflow.com/questions/514118/how-does-duffs-device-work – Sjoerd Mar 06 '17 at 08:15
  • 2
    The indentation of this code is outright hostile to the readers. – Kerrek SB Mar 06 '17 at 08:22

3 Answers3

4

The if statement works fine in the block of a switch, you've just put it in a place where it never gets executed. The switch statement jumps to the corresponding case, that is its purpose. Whatever it jumps to, it will skip over the if, so the if never executes.

If you were to add code to make it possible to jump to between the switch and the if, then the if would execute normally. You could do this with a loop of any kind or a goto.

A default is only taken if no case is matched. Otherwise, the switch jumps to the matching case.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
2

The switch statement doesn't work the way you imagine that it works. The way it behaves is not as a substitute of an if-else if-else, but somewhat differently.

Upon encountering the switch the process will jump to the code following the correct case. This means that you actually completely skip the execution of the if that you have placed there.

And yes, it does look weird, since you assume that because you have the curly braces you must execute if condition or not enter into them at all, but this is simply not the case.

v010dya
  • 5,296
  • 7
  • 28
  • 48
0

Well the placement of your if-statement doesn't really make sense. The switch-statement jumps to the corresponding case and then exists, hence your if-statement won't be executed. And I wouldn't recommend to use goto, cause it's considered as a bad-practice.

HS-Boy
  • 1