C++ 将1d阵列与用户输入进行比较?

C++ 将1d阵列与用户输入进行比较?,c++,arrays,random,numbers,C++,Arrays,Random,Numbers,我正在写一个测验程序。我有一个文本文件中的一维正确答案数组,我必须将其与用户的猜测进行比较,以检查他的猜测是否正确。我必须随机提出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

我正在写一个测验程序。我有一个文本文件中的一维正确答案数组,我必须将其与用户的猜测进行比较,以检查他的猜测是否正确。我必须随机提出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循环,因此您将按两个进行计数。

好的,因此您希望从50个问题中随机抽取6个问题,逐一呈现,如果用户错误两次,则结束程序,否则继续,直到他回答6个问题为止。您应该询问您的老师。这就是她在那里的目的。至于比较数组,您需要一次比较一个项目;很遗憾,您无法比较整个阵列。好吧,这很有帮助。我可以把更多的数据读入数组,一次比较一个。我实际上是自己做这件事的。我哥哥说这个项目真的帮助他提高了编码水平。他是在大学里做的。我只有14岁我想让他知道我很好。。通过问题和答案,我可以将它们作为随机数使用…我知道你在第一个版本中说了什么,你将问题和答案直接输入到程序中。一旦你从打印问题和得到答案中排除了所有的bug,你就可以进入第二个版本了。在第二个版本中,您有两个文件,一个有五十个问题,另一个有五十个答案。您可以通过读取这两个文件来加载数组。当你从中找出错误时,你会进入第三个版本。使用一个文件,问答交替。通过读取备用行来加载数组。在最新版本中,您可以在文件中包含任意数量的问答。