Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!