Home > Net >  Switch statements range of numbers in c
Switch statements range of numbers in c

Time:09-13

I have an assignment from my instructour he asked me to use switch statements to determine the age of the person, but I cannot find the error here, I did the same as I learned.

    //check user age
// error begins here
    switch (age)
    {
        case 0 ... 10:
            puts("You are less than 10");
            break;
        case 10 ... 20:
            puts("You are in your tens");
            break;
        case 20 ... 30:
            puts("You are in twenties");
            break;
        case 30 ... 40:
            puts("Your are in thirties");
            break;
        case 40 ... 50:
            puts("Your are in fourties");
            break;
        case 50 ... 60:
            puts("Your are in fifties");
            break;
        case 60 ... 70:
            puts("Your are in sixties");
            break;
        case 70 ... 80:
            puts("Your are in seventies");
            break;
        case 80 ... 90:
            puts("Your are in eighties");
            break;
        case 90 ... 100:
            puts("Your are in nineties");
            break;
        case 100:
            puts("Your are a 100  !!");
            break;
    
    }

**Errors by the compiler are ** :

Lab2_3.c:48:9: error: this is the first entry overlapping that value
         case 80 ... 90:
         ^~~~

CodePudding user response:

You can't use the switch statement for arbitrary ranges.

But since you only need to test decades, you can divide the age by ten to compare them.

switch (age/10)
{
    case 0:
        puts("You are less than 10");
        break;
    case 1:
        puts("You are in your tens");
        break;
    case 2:
        puts("You are in twenties");
        break;
    case 3:
        puts("Your are in thirties");
        break;
    case 4:
        puts("Your are in fourties");
        break;
    case 5:
        puts("Your are in fifties");
        break;
    case 6:
        puts("Your are in sixties");
        break;
    case 7:
        puts("Your are in seventies");
        break;
    case 8:
        puts("Your are in eighties");
        break;
    case 9:
        puts("Your are in nineties");
        break;
    default:
        puts("Your are a 100  !!");
        break;

}

CodePudding user response:

case 0 ... 10: is not portable C. Perhaps some compilers may support it as an extension.

Makes sense not to overlap the range

    case 0 ... 9: // not 10
        puts("You are less than 10");
        break;
    case 10 ... 19: // not 20
        puts("You are in your tens");
        break;
  ...

Instead of using a non-standard switch, case, consider:

if (age < 0) {
  puts("You are less than 0");
} else if (age < 10) {
  puts("You are less than 10");
} else if (age < 20) {
  puts("You are in your tens");
...

If a switch is still required:

    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
        puts("You are less than 10");
        break;
    case 11:
    case 12:
    ...

Or perhaps

if (age < 0) {
  puts("You are less than 0");
} else if (age < 100) {
  static const char *string_lookup_table[10] = {
    "less than 10", 
    "in your tens", 
    ...
  };
  int decade = age/10;
  printf("You are %s\n", string_lookup_table[decade]);
} else  {
  puts("You are a 100  !!");
}

Note: `"your" should be "you".

CodePudding user response:

First of all, please note that so-called "case ranges" is not valid standard C, but a non-standard gcc extension. As such, practicing to using them as a beginner is questionable at best. You will need to use the gcc compiler and use it in "lax" mode, without any -std=cxx or -pedantic flags. -std=gnu17 explicitly enables these kind of extensions.

As for the compiler errors, they are pretty self-explanatory. You have overlapping ranges, which isn't allowed by this extension. You need to do 0 ... 9 then 10 ... 19 and so on.

Algorithm-wise, ask yourself if a 10 year old is less than 10 years old. Is a 30 year old really in their twenties, and so on. That is not a programming problem.

Regarding your options to do this in valid standard C, see the answer by @chux.

  • Related