案例标签在C中没有减少到整数常量?

我正在开发一个游戏,我运行了我的代码并得到错误“案例标签不会减少到一个整数常量。” 我想我知道这意味着什么,但我该如何解决呢? 这是我的代码:

#include #include 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; } 

因此,我希望用户做的是键入其中一个选项,并执行与其对应的操作。 我该如何解决?

您的一些案例标签是字符(类型为char ,用' s表示)。 那些整数常量。

其他标签是字符串文字(用"表示),其有效类型为const char * .1这些不是整数常量,不能以这种方式使用。


1由于历史原因,它们通常可以像使用char * ,但不要尝试更改它们。 要不然。

您不能将字符串用作case表达式:

 case "Donate": 

只能使用整数表达式,例如case 'A':是的。

从概念上讲,您遇到了问题: jobs是一个int ,而您正在测试字符串。 如果你想允许用户输入字符串(多个单个字符),你需要保留一个字符串变量,并使用像fgets这样的东西来获得一整行输入。

你不能将字符串与c进行比较。 "hello" == "hello"不会按预期工作。 switch只对基本类型进行简单的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()一个字符,你可以比较这个值。 (但要求用户只输入一个字母,因为他输入“Donate”,getch()将首先读取D,返回,然后读取o等。)

  1. 您的作业数组具有不一致的初始值设定项(mixed charconst char *

  2. 您不能使用字符串文字作为案例标签,因为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; } 
  3. 此外, player_cash是1个元素短(并且在索引[3]处写出了界限)

代码示例还展示了如何避免一般性gets不良,做一些基本的行端修剪并进行不区分大小写的比较( stricmp的Windows,IIRC): http : stricmp

 #include #include #include 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; }