好的,我想将一个单词保存在一个char数组中,但它给了我一个错误。
这是我的代码:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char yesno[30] = "n"; //yes/no answer
char class[30] = "undefined";//choosen class
int classchoosen = 0;
/* initialize random seed: */
srand ( time(NULL) );
printf("Welcome, which class do you wanna play with? \n");
printf("Type W for Warrior, M for Mage or R for Ranger. Then press Enter\n");
while(classchoosen == 0)
{
scanf("%s", class);
if(strcmp(class, "W") == 0)
{
classchoosen = 1;
class = "Warrior";
}
if(strcmp(class, "M") == 0)
{
classchoosen = 1;
class = "Mage";
}
if(strcmp(class, "R") == 0)
{
classchoosen = 1;
class = "Ranger";
}
if(classchoosen == 0)
{
class = "undefined";
}
printf("So you wanna play as a %s? Enter y/n", class);
classchoosen = 0; //For testing, remove later
}
while(1)
{
/* Irrelevant stuff */
}
}
它给了我以下错误:
damagecalc.c:44:13: error: expected identifier
class -> "warrior";
^
damagecalc.c:49:10: error: array type 'char [30]' is not assignable
class = "mage";
~~~~~ ^
damagecalc.c:54:10: error: array type 'char [30]' is not assignable
class = "ranger";
~~~~~ ^
damagecalc.c:58:10: error: array type 'char [30]' is not assignable
class = "warlock";
~~~~~ ^
4 errors generated.
我知道我可以在字符串比较后立即打印出类名,但这件事真的让我很烦恼,我想知道为什么它不起作用。
PS:请原谅我可能犯的任何明显错误,我最近才开始从事PC编程,之前在uC上工作了几年。
char yesno='n';
而是使用char yesno[30] = "n";
? - Meninx - メネンックス