jeudi 5 août 2021

Convert multiple if else to switch case in C/ C++

In C/C++ I have scenario where if should be executed on the basis of empty size. If size of variable a is 0 then string 'add name' should be printed otherwise 'leave it' string should be printed. But I have switch cases for encoding as well for each case there will be different if condition.

 switch(encoding)
    case utf:
        if(a->size-5 == 0)
        {
            cout<<"add name";
        }
        else
        {
           cout<<"leave it";
        }
    case ansi:
        if(a->size-4 == 0)
        {
            cout<<"add name";
        }
        else
        {
           cout<<"leave it";
        }
    case ansi8:
        if(a->size-8 == 0)
        {
            cout<<"add name";
        }
        else
        {
           cout<<"leave it";
        } 

I want to do it in minimal way. So what is the best way to do that.

Aucun commentaire:

Enregistrer un commentaire