将1d数组与用户输入进行比较
comparing 1d array to users input?
我正在写一个智力竞赛程序。我有一个来自文本文件的正确答案的一维数组,我必须将其与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循环中又增加一次。
相关文章:
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 使用-L和-L标志与将库文件作为输入进行比较
- 通过与输入进行比较来安排输出
- 将私有对象变量与用户输入的变量进行比较
- 输入迭代器的相等性比较
- 将用户输入的术语与 txt 文件中的列表进行比较
- 将 ProcessEntry32.szExeFile 与用户输入的数据进行比较时,C++ _wcsicmp代码未编译
- 如何使用文件填充数组并将其与用户输入C 进行比较
- 从类中查看 rand() 并将用户输入与计算机选择进行比较.石头,纸,剪刀游戏
- (C++) 学习向量 - 编码/输入流终止/比较向量的问题
- 在比较特定输入中的程序的数组中出错
- 将最大最小值的数据与可变数量的输入进行比较 C++
- c++ 输入一个数字来确定其他数字,然后比较它们
- 比较输入的C弦与文件读取c-string
- 将用户输入与矢量中存储的值进行比较
- C++ - 读取和比较用户输入
- 将结果与用户输入-SQLITE3 C 进行比较
- 正在检查用户的有效输入并与文本文件进行比较
- 比较输入值和从文件中读取值的最佳方式