c++ - Switch with range of values -


so determine base salary employee based on inputed number of years employee has worked company.

i have using switch structures , given ranges follows:

number of years worked____base salary

 0 – 5                  $ 9,500   6 – 11                 $12,700  12 – 17                 $15,300  18 – 29                 $22,600   >= 30                  $26,800 

so, how do ranges cases if don't want right case of numbers?

it isn't of hassle one, have figure out commission based on sales , has ranges $0-3,999.99 , $16,000-23,999.99.

so part one, need declare switch multiple cases follow 1 code path. this:

int basesalary switch (yearsworked) {   case 0:   case 1:   case 2:   case 3:   case 4:   case 5:     basesalary = 9500;     break;    case 6:   case 7:   case 8:   case 9:   case 10:   case 11:     basesalary = 12700;     break;    ... etc ... } 

for second part, switch every single number in range of thousands pretty unfeasible, bit of smart division, can made equally easy. if divide 2000 1000, 2, , if divide 2500 1000, 2 (with remainder 500). using this, can generate switch statement:

int sales = 2100; int salesrange = sales / 1000; // (salesrange = 2)  int commission switch (salesrange) {   case 0:  // $0-999 sales   case 1:  // $1000-1999 sales   case 2:  // $2000-2999 sales   case 3:  // $3000-3999 sales     commission = <some number here>;     break;    ... etc ... } 

that being said, assumes "have use switch" part of school assignment or similar. other people have mentioned, you're better off using if statements range (e.g. if (sales >= 0 && sales <= 3999)) using switch kind of thing.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -