Grasshelicopter
Grasshelicopter

Reputation: 17

error: case label does not reduce to an integer constant when using character data type

This is the projects 4 of Chapter 7:Basic Types I am trying to do in the book "C Programming: A Modern Approach(2nd version)". The question looks like this: Question description here

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

int main(){
    char apb;
    int num;
    printf("Enter phone number:");
    do{
    apb = getchar();
    apb = toupper(apb);
    while(apb != ' ' || apb != '\n'){
        switch(apb){
            case 'A': case'B': case "C":
                printf("2");break;
            case 'D': case'E': case "F":
                printf("3");break;
            ...
            case 'Y': case'W': case "X":
                printf("9");break;
            default:printf("Error:please try again\n");
        }
    }
}while{apb >='A' && apb <='Z'};
    return 0; 
}

when i try to compile the code, the compiler threw a error for every part for my case label saying that:

"[Error]case label does not reduce to an integer constant".

it made me kinda for curious that what kind of mistake that i have made as i see no error in using a constant character type as a case label. Is it about the version of compiler that i am using?

Upvotes: 0

Views: 126

Answers (1)

paxdiablo
paxdiablo

Reputation: 882226

case 'A': case'B': case "C":

While 'A' and 'B' are characters, "C" is a character pointer so cannot be used.

You need to use 'C' instead. Ditto for F and X.

Upvotes: 2

Related Questions