我正在寫一個測驗程序。我有一個來自文本文件的正確答案的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
}
}
我的麻煩是越來越多的問題與答案數組掛鉤。另外,如果他錯了兩次,程序結束。你們都在想什麼?
好吧,所以你想從50個池中隨機抽取6個問題,逐個呈現,如果用戶錯誤兩次結束程序,否則繼續,直到他回答6? – whitelionV 2012-02-08 06:17:02
你應該問問你的老師。這就是他在那裏的原因。至於比較數組,你需要一次比較一個項目;不幸的是,你無法比較整個陣列。 – Zenexer 2012-02-08 06:17:47
好吧,幫助。我可以讀一些更多的數組來比較他們一次一個。 – gamergirl22 2012-02-08 07:02:30