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

时间:2012-02-08 06:13:21

标签: c++ arrays random numbers

我正在写一个测验程序。我有一个来自文本文件的一维正确答案数组,我必须将其与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
    }
}

我的麻烦是让一系列问题与答案数组联系在一起。此外,如果他错了两次,结束该计划。你们都在想什么?

1 个答案:

答案 0 :(得分: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循环中再增加一次,所以你将按两次计算。