2012-02-08 101 views
0

我正在寫一個測驗程序。我有一個來自文本文件的正確答案的1維數組,我必須將其與users_guess進行比較以檢查他的猜測是否正確。我必須吐出6個隨機問題。將1d數組與用戶輸入進行比較?

string questions[50]; // 50 questions 
char answers[50]; // 50 correct answers 
int i = 0; 
char user_guess; 

int rand_index = rand() % 10; //generate random number 

for (i=0; i<6; i++)  //loop for 6 questions 
{  
cout << questions[rand_index] << endl; 
questions[rand_index] = answers[] // i need help. how do i compare the arrays? 
cin >> user_guess; 
    if (user_guess != answers[]) // if he's wrong 
    { 
    cout << "sorry. try again" << endl; 
    cout << questions[rand_index] << endl; // 2nd chance 
    cin >> user_guess; 
     if (user_guess!= answers[]) // wrong again 
     { 
     cout << "you lose.game over." << endl; //game over 
     break; // does this cancel the game all together? 

     } 
     else 
     { 
     cout << "good job!" << endl; 
     i++; // on to the next round 
     } 
    } 
    else 
    { 
    cout << "good job!" << endl; 
    i++; // on to the next round 
    } 
} 

我的麻煩是越來越多的問題與答案數組掛鉤。另外,如果他錯了兩次,程序結束。你們都在想什麼?

+0

好吧,所以你想從50個池中隨機抽取6個問題,逐個呈現,如果用戶錯誤兩次結束程序,否則繼續,直到他回答6? – whitelionV 2012-02-08 06:17:02

+1

你應該問問你的老師。這就是他在那裏的原因。至於比較數組,你需要一次比較一個項目;不幸的是,你無法比較整個陣列。 – Zenexer 2012-02-08 06:17:47

+0

好吧,幫助。我可以讀一些更多的數組來比較他們一次一個。 – gamergirl22 2012-02-08 07:02:30

回答

0
Here's a hint: 

// ... 
{ 
    const string &the_question = questions[rand_index]; 
    const char &the_answer = answers[rand_index]; // using a const char & 
               // is a deliberate pedantism 


    cout << the_question << endl; 
    char user_guess; 
    cin >> user_guess; 
    if (the_answer != user_guess) { 
    ... 
    } 

注意:你在一個好的工作後再增加一次,然後再次在for循環中,所以你會以兩個數來計算。

+0

好吧聲明2常量..通過the_question和the_answer允許我使用它們作爲隨機...我看到你說什麼 – gamergirl22 2012-02-08 07:06:38

+0

在第一個版本中,你把問題和答案放在程序中。一旦你把所有的錯誤都打印出來然後得到答案,你就轉到第二個版本。在第二個版本中,你有兩個文件,一個包含50個問題,另一個包含50個答案。你可以通過閱讀這兩個文件來加載你的數組。當你發現錯誤時,你會轉到第三個版本。這使用一個文件,交替出現問題和答案。您可以通過讀取備用行來加載陣列。最後一個版本中,您可以在文件中添加任意數量的q&a。 – 2012-02-09 06:02:01

相關問題