将1d数组与用户输入进行比较

comparing 1d array to users input?

本文关键字:比较 输入 用户 1d 数组      更新时间:2023-10-16

我正在写一个智力竞赛程序。我有一个来自文本文件的正确答案的一维数组,我必须将其与users_guess进行比较,以检查他的猜测是否正确。我必须随机提出6个问题。

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

我的问题是把一系列的问题和一系列的答案联系起来。此外,如果他错了两次,就结束这个节目。你们怎么想?

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循环中又增加一次。