Home > Software engineering >  Having an error case label does not reduce to an integer constant using switch in C
Having an error case label does not reduce to an integer constant using switch in C

Time:09-07

I'm trying to learn C. I try also to code this program that is supposed to do :

The program chooses a random number between 0 and 100 and the user has to guess which integer was chosen. Each time the user proposes an integer, the program should print :

'High' if the guessing number is higher than the chosen random number, and the user has the possibility to try again by entering again a new value

'Low' if the guessing number is lower than the chosen random number, and the user has the possibility to try again by entering again a new value

'Exact' if the guessing number is equal to the chosen random number

Here is my code :

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
{
    int guess;
    srand(time(NULL));
    int x = rand() % 100;
    //printf("Random value is %d \n", x);
    printf("Guessing value: \n");

    scanf("%d", &guess);

    switch (guess)
    {
    case guess < x:
        printf("High \n");
        scanf("%d", &guess);
        break;
    case guess > x:
        printf("Low \n");
        scanf("%d", &guess);
        break;
    case guess == x:
        printf("Exact");
        break;
    default:
        break;
    }

    return 0;
}

Can someone see why do I get :

error: case label does not reduce to an integer constant

Other try but the srand in the if doesn't work:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
{
    int guess;
    srand(time(NULL));
    int x = rand() % 100;
    //printf("Random value %d \n", x);
    printf("Guessing value: \n");

    scanf("%d", &guess);

    if (guess < x)
    {
        printf("High \n");
        scanf("%d", &guess);
    }
    else if (guess > x)
    {
        printf("Low \n");
        scanf("%d", &guess);
    }
    else
    {
        printf("Exact");
    }

    return 0;
}

CodePudding user response:

switch ... case does not work like this. You need to use if statements.

    if(guess < x)
    {
        printf("High \n");
    }
    else if(guess > x)
    {
        printf("Low \n");
    }
    else
        printf("Exact");

case integer_constant_expression: - also the integer_constant_expression has to have distinct value ie the value case to be unique in every case statement.

CodePudding user response:

According to the C Standard (6.8.4.2 The switch statement)

3 The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion. There may be at most one default label in a switch statement. (Any enclosed switch statement may have a default label or case constant expressions with values that duplicate case constant expressions in the enclosing switch statement.)

and (6.6 Constant expressions)

6 An integer constant expression117) shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.

In this switch statement from the first program

switch (guess)
{
case guess < x:
    printf("High \n");
    scanf("%d", &guess);
    break;
case guess > x:
    printf("Low \n");
    scanf("%d", &guess);
    break;
case guess == x:
    printf("Exact");
    break;
default:
    break;
}

case labels are not integer constant expressions. So the compiler issues an error.

Instead you could write using the switch statement the following way. Pay attention to that you need to use a loop. Otherwise the user will have only one attempt.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main( void )
{
    int success = 0;
    srand( ( unsigned )time( NULL ) );
    int x = rand() % 100;

    do
    {
        int guess;
        printf("Guessing value: \n");

        scanf( "%d", &guess );
     
        success = ( x < guess ) - ( guess < x );

        switch ( success )
        {
        case 1:
            printf("High \n");
            break;
        case -1:
            printf("Low \n");
            break;
        case 0:
            printf("Exact");
            break;
        }
    } while ( success != 0 );

    return 0;
}

You could also to introduce named constant for values 0, -1, and 1 as for example

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main( void )
{
    enum { Exact = 0, Low = -1, High = 1 };

    srand( ( unsigned )time( NULL ) );
    int x = rand() % 100;

    int success = Exact;

    do
    {
        int guess;
        printf( "Guessing value: \n" );

        scanf( "%d", &guess );
     
        success = ( x < guess ) - ( guess < x );

        switch ( success )
        {
        case High:
            printf("High \n");
            break;
        case Low:
            printf("Low \n");
            break;
        case Exact:
            printf("Exact");
            break;
        }
    } while ( success != Exact );

    return 0;
}
  • Related