Home > Software engineering >  Can anyone clarify the error in my switch statement?
Can anyone clarify the error in my switch statement?

Time:09-19

I keep getting this error for my switch statement when I compile it:

"scrabble.c:50:9: error: statement requires expression of integer type ('string' (aka 'char *') invalid) switch (word) ^ ~~~~ fatal error: too many errors emitted, stopping now [-ferror-limit=]"

#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>

// complete the compute_score function
// return the number of points for the word
// ignore non-letter words
// handle both upper-case and lower-case letters
// in main, Print: "player 1 wins!" or "player 2 wins!" or "Tie!".

// Points assigned to each letter of the alphabet
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 
10};

int compute_score(string word);

int main(void)
{
    // Get input words from both players
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    // Score both words
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);

    // TODO: Print the winner
    if (score1 > score2)
    {
        printf("Player 1 Wins!\n");
    }
    else if (score1 < score2)
    {
        printf("Player2 Wins!\n");
    }
    else
    {
        printf("It's a TIE!");
    }
}

int compute_score(string word)
{
    // TODO: Compute and return score for string
    int score = 0;
    int length = strlen(word);

    for (int i = 0; i < length; i  )
   {
        switch (word)
        {
        case isupper(word[i]):
            score  = Points(word[i] - '65');
            Break;
        case islower(word[i]):
            score  = Points(word[i] - '97');
            Break;
        }
        return score;
    }
}

CodePudding user response:

The error message means what it says: A switch statement requires an expression of integer type.

word is not an integer type. It is a string, which is defined to be char *. You cannot use a switch statement to select among abstract notions or computed expressions in the cases. You can only use a switch statement if you can give a specific integer value to be used for the selection.

Rewrite your program to use if statements instead of a switch.

CodePudding user response:

The variable word has the type string that is a typedef name for the pointer type char *. But the expression in the switch statement shall have an integer type. So this switch statement

switch (word)

is incorrect.

Also case labels shall have integer constant expressions. Thus these case labels

case isupper(word[i]):

and

case islower(word[i]):

are incorrect because the function calls isupper and islower return run-time values that are not integer constant expressions.

From 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.)

Use if-else statement instead of the switch

    if ( isupper( ( unsigned char )word[i]) )
    {
        score  = Points(word[i] - 'A');
    }
    else if islower( ( unsigned char )word[i] )
    {
        score  = Points(word[i] - 'a');
    }

Also place the return statement outside the for loop

for (int i = 0; i < length; i  )
{
    //...
}
return score;

Pay attention to that the call of the function strlen is redundant. You could write the for loop like

for ( ; *word != '\0';   word )
{
    if ( isupper( ( unsigned char )*word ) )
    {
        score  = Points( *word - 'A');
    }
    else if islower( ( unsigned char )*word )
    {
        score  = Points( *word - 'a');
    }
}

return score;
  • Related