2014-01-30 36 views
1

我想寫一個關於arduino的5個問題測驗,它有三個可能的答案,並在最後輸出一個分數。我試圖在提出第一個問題的地方寫下它,然後在有人輸入他們的答案後,進入下一個問題。我在「。」之前遇到初始化程序有問題。令牌錯誤消息。你可以幫我嗎?如何在arduino上寫測驗

`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

您的代碼中有很多錯誤。

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

我有之前與初始化麻煩 「」令牌錯誤 消息

一旦聲明中的數據正確這些錯誤將得到解決。不幸的是,你會有其他錯誤進一步下降,但我會留給他們(應該很容易修復)。