1

我正在尝试在 arduino 上写一个 5 个问题的测验,其中包含三个可能的答案并在最后输出一个分数。我试图把它写在它问第一个问题的地方,在有人输入他们的答案后,它会转到下一个问题。我在“。”之前的初始化程序有问题。令牌错误消息。你能帮助我吗?

`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]:

}`
4

1 回答 1

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'
  }  
};

我在“。”之前的初始化程序有问题。令牌错误消息

正确声明数据后,这些错误将得到解决。不幸的是,您还会遇到其他错误,但是我将它们留给您(应该很容易修复)。

于 2014-10-13T09:51:25.873 回答