Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda