Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
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.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I don't fully understand what solution you are expecting, but - same as suggested in a comment - when removing duplication from your code we are left with:

    if(a->size-offset == 0)
    {
        cout<<"add name";
    }
    else
    {
       cout<<"leave it";
    }

Where offset can be determined via:

int offset = 0;
switch(encoding) {
   case utf: offset = 5; break;
   case ansi: offset = 4; break;
   case asni8: offset = 8; break;
}

Probably a cleaner solution would be to use a polymorphic type such that differences in the encoding are encapsulated in virtual methods, and you can write:

    if(a->check_size())
    {
        cout<<"add name";
    }
    else
    {
       cout<<"leave it";
    }
over 4 years ago · Santiago Trujillo Relatório

0

I think that ternary operator is the best approach.

 switch(encoding) {
    case utf:
        (a->size - 5 == 0) ? std::cout << "add name" : std::cout << "leave it";
        break;
    case ansi:
        (a->size - 4 == 0) ? std::cout << "add name" : std::cout << "leave it";
        break;
    case ansi8:
        (a->size - 8 == 0) ? std::cout << "add name" : std::cout << "leave it";
        break;
 }
over 4 years ago · Santiago Trujillo Relatório

0

I'd write your code like this:

//...
//assuming utf, ansi, and ansi8 are enumerated constants
static const int encoding2size[]={ [utf]=5,[ansi]=4,[ansi8]=8 };
//...
if(a->size - encoding2size[encoding] == 0) cout<<"add name";
else cout<<"leave it";
//...
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda