сравнивать 1d массив с пользовательским вводом? - PullRequest
0 голосов
/ 08 февраля 2012

Я пишу программу викторины. У меня есть одномерный массив правильных ответов из текстового файла, который я должен сравнить с 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 голосов
/ 08 февраля 2012
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, так что вы будете считать по двое.

...