案例标签不减少在C整型常量? [英] Case label does not reduce to an integer constant in C?
问题描述
我工作的一个游戏,我跑我的code和得到了错误的情况下标签不降低到整型常量。我想我知道这意味着什么,但我怎么解决?这里是我的code:
I am working on a game and I ran my code and got the error "case label does not reduce to an integer constant." I think I know what this means, but how do I fix it? Here is my code:
#include<stdio.h>
#include<stdlib.h
int player_cash[3] = {50};
char job[][20] {
'A',
'B',
'C',
"Donate",
"Go to work",
"Exit"
};
int jobs;
int main()
{
while(player_cash[0] > 0) {
printf("Please type A, B, C, Donate, Go to work, or Exit\n");
switch(jobs) {
case 'A':
player_cash[0]-=5;
player_cash[1]+=5;
printf("Cash=%i\n\n", player_cash[0]);
continue;
case 'B':
player_cash[0]-=5;
player_cash[2]+=5;
printf("Cash=%i\n\n", player_cash[0]);
continue;
case 'C':
player_cash[0]-=5;
player_cash[3]+=5;
printf("Cash=%i\n\n", player_cash[0]);
continue;
case "Donate":
player_cash[0]-=15; //Error here
player_cash[1]+=5;
player_cash[2]+=5;
player_cash[3]+=5;
printf("Cash donated\n\n");
printf("Cash=%i\n\n", player_cash[0]);
continue;
case "Go to work":
player_cash[0]+=10; //Error here
printf("Work done\n\n");
printf("Cash=%i\n\n", player_cash[0]);
continue;
case "Exit":
printf("Thanks for playing!\n\n"); //Error here
break;
default:
printf("Does not compute");
continue;
}
}
getchar();
return 0;
}
所以,我想要什么,用户需要做的就是类型的选项之一,做到这一点与它对应的动作。我该如何解决这个问题?
So, what I want the user to do is type in one of the options, and do the action that corresponds with it. How do I fix this?
推荐答案
你的一些情况标签是字符(键入字符
与表示'
S)。那些是的整型常量。
Some of your case labels are characters (type char
, indicated with '
s). Those are integer constants.
其他的标签字符串(以表示
),其有效类型的为const char *
。< SUP> 1 这些的不的整型常量,不能采用这种方式。
Other labels are string literals (indicated with "
) which have an effective type of const char *
.1 Those are not integer constants and can not be used in this way.
1 因为他们通常可以使用,如果他们的char *
,但不要试图去改变的历史原因。否则后果不堪设想。
1 For historical reasons they can often be used as if they were char *
, but don't try to change them. Or else.
这篇关于案例标签不减少在C整型常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!