在C中,大小写标签不能减少为整数常量?
我正在做一个游戏,我运行了我的代码,得到了错误“case label没有减少到整数常量”。我想我知道这意味着什么,但我如何修复它?这是我的密码:在C中,大小写标签不能减少为整数常量?,c,arrays,label,switch-statement,case,C,Arrays,Label,Switch Statement,Case,我正在做一个游戏,我运行了我的代码,得到了错误“case label没有减少到整数常量”。我想我知道这意味着什么,但我如何修复它?这是我的密码: #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
#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;
}
#包括
#包括(0){
printf(“请键入A、B、C、捐赠、上班或退出”);
转换(工作){
案例“A”:
玩家_cash[0]=5;
玩家_cash[1]+=5;
printf(“Cash=%i\n\n”,player_Cash[0]);
继续;
案例“B”:
玩家_cash[0]=5;
玩家_cash[2]+=5;
printf(“Cash=%i\n\n”,player_Cash[0]);
继续;
案例“C”:
玩家_cash[0]=5;
玩家_cash[3]+=5;
printf(“Cash=%i\n\n”,player_Cash[0]);
继续;
“捐赠”案例:
player_cash[0]-=15;//此处出错
玩家_cash[1]+=5;
玩家_cash[2]+=5;
玩家_cash[3]+=5;
printf(“捐赠现金”\n\n);
printf(“Cash=%i\n\n”,player_Cash[0]);
继续;
“上班”案例:
player_cash[0]+=10;//此处出错
printf(“已完成的工作\n\n”);
printf(“Cash=%i\n\n”,player_Cash[0]);
继续;
案例“退出”:
printf(“感谢播放!\n\n”);//此处出错
打破
违约:
printf(“不计算”);
继续;
}
}
getchar();
返回0;
}
因此,我想让用户做的是输入其中一个选项,并执行与之对应的操作。如何解决此问题?不能将字符串用作大小写表达式:
case "Donate":
只能使用整数表达式,因此例如案例“A”:
可以
从概念上讲,您有问题:
jobs
是一个int
,您正在测试字符串。如果您想允许用户输入字符串(多于一个字符),则需要保留一个字符串变量,并使用类似于fgets
的内容来获取完整的输入行。一些大小写标签是字符(键入char
,用'
s表示)。这些是整数常量
其他标签是字符串文字(用“
”表示),其有效类型为常量字符*
。1这些不是整型常量,不能以这种方式使用
1由于历史原因,它们通常可以像使用字符一样使用,但不要试图更改它们。否则。您无法将字符串与c进行比较。
“hello”==“hello”
无法按预期工作。开关仅对基本类型进行简单的c比较
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 'D':
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 'G':
player_cash[0]+=10; //Error here
printf("Work done\n\n");
printf("Cash=%i\n\n", player_cash[0]);
continue;
case 'E':
printf("Thanks for playing!\n\n"); //Error here
break;
default:
printf("Does not compute");
continue;
}
由于您只能在getch()
中读取一个字符,因此可以比较此值。(但要求用户只输入一个字母,因为他输入了“捐赠”,getch()将首先读取D,返回,然后读取o,等等)
char
和const char*
)enum jobType
{
jobA,
jobB,
jobC,
jobDonate,
jobGoToWork,
jobExit,
/* marker */
jobInvalid
};
enum jobType typeOfJob(const char* const name)
{
int i;
for (i=jobA; i!=jobInvalid; ++i)
if (0 == strcmp(jobNames[i], name))
return i;
return i;
}
player\u cash
短了1个元素(并且在索引[3]处被写得越界)代码示例还显示了如何避免常规的
gets
不良,进行一些基本的行尾修剪,并进行实例比较(stricmp
在windows上,IIRC):
#包括
#包括
#包括
int player_cash[4]={50};
枚举作业类型
{
若巴,
乔布,
乔布,
工作捐赠,
jobGoToWork,
工作出口,
/*标记*/
工作无效
};
常量字符作业名[][20]=
{
“A”,
“B”,
“C”,
“捐赠”,
“去工作”,
“退出”
};
枚举作业类型作业类型(常量字符*常量名称)
{
int i;
for(i=jobA;i!=jobInvalid;++i)
#ifdef区分大小写
if(0==strcmp(作业名称[i],名称))
#否则
if(0==stracecmp(作业名称[i],名称))
#恩迪夫
返回i;
返回i;
}
const char*safer_gets()
{
静态字符输入[1024];
char*p;
常量字符*t;
常量字符trimAt[]=“\r\n\t”;
fgets(输入,sizeof(输入),标准输入);
对于(t=trimAt;*t;++t)
而(p=strrchr(输入,*t))
*p=0;
返回输入;
}
int main()
{
常量字符*输入;
同时(玩家\现金[0]>0)
{
printf(“请键入A、B、C、捐赠、上班或退出”);
input=safer_get();
开关(作业类型(输入))
{
案例A:
玩家_cash[0]=5;
玩家_cash[1]+=5;
printf(“Cash=%i\n\n”,player_Cash[0]);
继续;
个案b:
玩家_cash[0]=5;
玩家_cash[2]+=5;
printf(“Cash=%i\n\n”,player_Cash[0]);
继续;
案例C:
玩家_cash[0]=5;
玩家_cash[3]+=5;
printf(“Cash=%i\n\n”,player_Cash[0]);
继续;
个案:
玩家现金[0]=15;
玩家_cash[1]+=5;
玩家_cash[2]+=5;
玩家_cash[3]+=5;
printf(“捐赠现金”\n\n);
printf(“Cash=%i\n\n”,player_Cash[0]);
继续;
个案工作:
玩家_现金[0]+=10;
printf(“已完成的工作\n\n”);
printf(“Cash=%i\n\n”,player_Cash[0]);
继续;
个案出口:
printf(“感谢您的播放!\n\n”);
打破
违约:
printf(“不计算”);
继续;
}
}
getchar();
返回0;
}
@Brandon,例如,你可以使用一个if
语句与strcmp
结合使用。你的程序中也有很多其他错误-例如,你实际上没有读取任何输入。@Brandon:if(strcmp(jobs,constant_1))…else if…
@Vlad,是的。我想这只是旧的命名法。修复了。@Brando
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int player_cash[4] = {50};
enum jobType
{
jobA,
jobB,
jobC,
jobDonate,
jobGoToWork,
jobExit,
/* marker */
jobInvalid
};
const char jobNames[][20] =
{
"A",
"B",
"C",
"Donate",
"Go to work",
"Exit"
};
enum jobType typeOfJob(const char* const name)
{
int i;
for (i=jobA; i!=jobInvalid; ++i)
#ifdef CASE_SENSITIVE
if (0 == strcmp(jobNames[i], name))
#else
if (0 == strcasecmp(jobNames[i], name))
#endif
return i;
return i;
}
const char* safer_gets()
{
static char input[1024];
char *p;
const char* t;
const char trimAt[] = "\r\n\t ";
fgets(input, sizeof(input), stdin);
for (t=trimAt; *t; ++t)
while(p = strrchr(input, *t))
*p = 0;
return input;
}
int main()
{
const char* input;
while(player_cash[0] > 0)
{
printf("Please type A, B, C, Donate, Go to work, or Exit\n");
input = safer_gets();
switch(typeOfJob(input))
{
case jobA:
player_cash[0]-=5;
player_cash[1]+=5;
printf("Cash=%i\n\n", player_cash[0]);
continue;
case jobB:
player_cash[0]-=5;
player_cash[2]+=5;
printf("Cash=%i\n\n", player_cash[0]);
continue;
case jobC:
player_cash[0]-=5;
player_cash[3]+=5;
printf("Cash=%i\n\n", player_cash[0]);
continue;
case jobDonate:
player_cash[0]-=15;
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 jobGoToWork:
player_cash[0]+=10;
printf("Work done\n\n");
printf("Cash=%i\n\n", player_cash[0]);
continue;
case jobExit:
printf("Thanks for playing!\n\n");
break;
default:
printf("Does not compute");
continue;
}
}
getchar();
return 0;
}