我正在尝试在arduino上写一个5个问题的测验,它有三个可能的答案并在结尾输出一个分数。我试着把它写在第一个问题的问题上,在有人输入他们的答案后,它会转到下一个问题。我在#34;之前遇到了初始化程序的问题。"令牌错误消息。你能救我吗?
`typedef struct{
char question[];
char answer1[];
char answer2[];
char answer3[];
char correct_answer;
}question_t;
question_t questions[] = {questions[0], questions[1], questions[2], questions[3], questions[4]};
question_t questions[0].question = "1. What is the capital of Illinois?";
question_t questions[0].answer1 = "a) Chicago";
question_t questions[0].answer2 = "b) Springfield";
question_t questions[0].answer3 = "c) Carbondale";
question_t questions[0].correct_answer = 'b';
question_t questions[1].question = "2. Who is the governor of Wisconsin?";
question_t questions[1].answer1 = "a) Scott Walker";
question_t questions[1].answer2 = "b) Clay Matthews";
question_t questions[1].answer3 = "c) Cole Nebel";
question_t questions[1].correct_answer = 'a';
question_t questions[2].question = "3. Where is MSOE?";
question_t questions[2].answer1 = "a) Memphis";
question_t questions[2].answer2 = "b) Manitowoc";
question_t questions[2].answer3 = "c) Milwaukee";
question_t questions[2].correct_answer = 'c';
question_t questions[3].question = "4. Why is Justin Bieber in jail?";
question_t questions[3].answer1 = "a) DUI";
question_t questions[3].answer2 = "b) bad music";
question_t questions[3].answer3 = "c) looking like Miley Cyrus";
question_t questions[3].correct_answer = 'a';
question_t questions[4].question = "5. Who is Jim Jefferies?";
question_t questions[4].answer1 = "a) actor";
question_t questions[4].answer2 = "b) singer";
question_t questions[4].answer3 = "c) comedian";
question_t questions[4].correct_answer = 'c';
int score;
int i;
char c;
char read_user_input(char c);
void output_question(uint8_t i);
void output_score(void);
loop()
{
init();
score=0:
uint8_t i;
i=0;
while(i<5)
{
output_question(i);
c=read_user_input();
if(c==questions[i].correct_answer)
{
score=score+20;
}i++;
}
Serial.print(output_score());
}
void init(void)
{
questions[5]:
}`
答案 0 :(得分:0)
您的代码中存在很多错误。
typedef struct{
char question[];
char answer1[];
char answer2[];
char answer3[];
char correct_answer;
}question_t;
您需要为数组提供一个长度。
question_t questions[] = {questions[0], questions[1], questions[2], questions[3], questions[4]};
question_t questions[0].question = "1. What is the capital of Illinois?";
question_t questions[0].answer1 = "a) Chicago";
question_t questions[0].answer2 = "b) Springfield";
question_t questions[0].answer3 = "c) Carbondale";
question_t questions[0].correct_answer = 'b';
这些声明是错误的。要初始化数组,您需要使用大括号括号初始化程序(第一行的内容)。否则,您使用的 object.member 语法需要在函数内部(没有前面的类型名称)。
要初始化数组,您需要在一个语句中执行此操作。
struct question_t{
char question[30];
char answer1[30];
char answer2[30];
char answer3[30];
char correct_answer;
};
question_t questions[] = {
{
"Question 1",
"Answer 1",
"Answer 2",
"Answer 3",
'a'
},
{
"Question 2",
"Answer 1",
"Answer 2",
"Answer 3",
'b'
},
{
"Question 3",
"Answer 1",
"Answer 2",
"Answer 3",
'c'
}
};
我在“。”之前遇到了初始化程序的问题。令牌错误 消息
一旦您正确地声明了数据,这些错误就会得到解决。不幸的是,你还会有其他错误,但是我会把它们留给你(应该很容易解决)。