如何防止用户多次选择一个选项
How to prevent user from picking an option more than once
我写了一个非常简单的问答c++程序(谁想成为百万富翁),它从文件中读取问题,我想包括一个特殊的帮助选项,使用时会跳过答案。问题是,我不希望用户多次使用该选项,我该怎么做呢。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string a[15]; //Array which will take answers
string print, print2; //Strings for printing questions
ifstream answers("q&a.txt");
cout << "Welcome to who wants to be a millionairen";
while (answers) //Read questions from the file
{
getline(answers, print);
auto position = print.find( "01." );
if (position <= print.size())
{
print2 = print.substr(position + 1 );
cout << print2 << endl << "A. Blackburn LosersnB. Blackburn RoversnC. Blackburn LoversnD. BlackburnWanderersn";
}
cin >> a[0];
if (a[0] == "b") //If correct, proceed to next question
{
getline(answers, print);
position = print.find("02.");
}
//If the answer is wrong, terminate the program
if (a[0] != "b")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. FridgenB. BanknC. MarketnD. Shoen";
}
cin >> a[1];
if (a[1] == "b")
{
getline(answers, print);
position = print.find("03");
}
if (a[1] != "b")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. BasseynB. KwesinC. AbiodunnD. Ejiman";
}
cin >> a[2];
if (a[2] == "b")
{
getline(answers, print);
position = print.find("04.");
}
if (a[2] != "b")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. InfernonB. DominonC. StilettonD. Tornadon";
}
cin >> a[3];
if (a[3] == "a")
{
getline(answers, print);
position = print.find("05.");
}
if (a[3] != "a")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. Marry a wifenB. Bury a dead parentnC. Have thanksgiving in churchnD. Accept gifts or favour in kindn";
}
cin >> a[4];
if (a[4] == "d")
{
getline(answers, print);
position = print.find("06.");
}
if (a[4] != "d")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. TenacitynB. VerifiabilitynC. HereditarynD. Validationn";
}
cin >> a[5];
if (a[5] == "c")
{
getline(answers, print);
position = print.find("07.");
}
if (a[5] != "c")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. ParisnB. CopenhagennC. New YorknD. Madridn";
}
cin >> a[6];
if (a[6] == "a")
{
getline(answers, print);
position = print.find("08");
}
if (a[6] != "a")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position +1);
cout << print2 << endl << "A. MumbainB. BeijingnC. Rio de JaneironD. Sevillen";
}
cin >> a[7];
if (a[7] == "b")
{
getline(answers, print);
position = print.find("09");
}
if (a[7] != "b")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. A speed equal to that of soundnB. A speed greater than that of soundnC. A speed equal to that of lightnD. A speed greater than that of lightn";
}
cin >> a[8];
if (a[8] == "b")
{
getline(answers, print);
position = print.find("10.");
}
if (a[8] != "b")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. NephronsnB. NervesnC. LigamentsnD. Stitchesn";
}
cin >> a[9];
if (a[9] == "c")
{
getline(answers, print);
position = print.find("11.");
}
if (a[9] != "c")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. SwimmernB. RefereenC. Football FannD. Judokan";
}
cin >> a[10];
if (a[10] == "b")
{
getline(answers, print);
position = print.find("12.");
}
if (a[10] != "b")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. FrancenB. United StatesnC. GermanynD. Indian";
}
cin >> a[11];
if (a[11] == "c")
{
getline(answers, print);
position = print.find("13.");
}
if (a[11] != "c")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. Clint EastwoodnB. Oliver StonenC. Peter JacksonnD. Morgan Freemann";
}
cin >> a[12];
if (a[12] == "a")
{
getline(answers, print);
position = print.find("14.");
}
if (a[12] != "a")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. LebanonnB. ColumbianC. JapannD. Eritrean";
}
cin >> a[13];
if (a[13] == "b")
{
getline(answers, print);
position = print.find("15.");
}
if (a[13] != "b")
{
cerr << "We are sorry, you are wrong!n";
break;
}
if (position <= print.size())
{
print2 = print.substr(position + 1);
cout << print2 << endl << "A. LiteraturenB. EconomicsnC. PeacenD. Medicinen";
cin >> a[14];
}
if (a[14] == "c")
{
cout << "Congratulations!nYou won a million dollars!n";
break;
}
if (a[14] != "c")
cerr << "We are sorry, you are wrong!n";
break;
}
return 0;
}
问题:
01. Which of these is the name of a British Football Club?
02. An establishment where money can be deposited or withdrawn is called what?
03 Name given to a boy born on Sunday in Ghana is what?
04. Which of the following refer to the word fire?
05. According to the constitution a public officer is not allowed to do which of these?
06. The process by which genetic traits are transmitted from parents to offspring is called what?
07. Roland Garros stadium is in which city?
08. Where is Tiananmen Square?
09. The word supersonic denotes which of these?
10. Which of these holds bones together at the joints of the body?
11. Linus Mbah achieved fame in Nigerian sporting circles as what?
12. DAX refers to the stock market of which country?
13. Who won the Academy Award for directing the movie ‘Million Dollar Baby’?
14. In which country is the Galeras Volcano?
15. Professor Maathai Wangari won the Nobel Prize for which of these?
答案:
1 (B)
2 (B)
3 (B)
4 (A)
5 (D)
6 (C)
7 (A)
8 (B)
9 (B)
10 (C)
11 (B)
12 (C)
13 (A)
14 (B)
I15 (C)
声明一个名为"usedHelp"的布尔值,并将其初始化为"false"。对每个问题进行测试
if (a[0] == "?" && (!usedHelp) ) // This means the user pressed the 'help' key (in this case, a question mark '?') and they haven't already used it.
{
usedHelp = true; // Now set the 'úsedHelp' variable to 'true'. The next time a user presses '?', it will skip a similar if statement
getline(answers, print);
position = print.find("02.");
}
首先,您必须将"香肠"代码压缩成一个循环。唯一不同的是可能的答案,以及其中哪一个是正确的。我建议你也把它放在文件中,所以文件看起来像这样:
Which of these is the name of a British Football Club?
Blackburn Rovers
Blackburn Losers
Blackburn Lovers
Blackburn Wanderers
An establishment where money can be deposited or withdrawn is called what?
Bank
Fridge
Market
Shoe
...
每个挑战需要5行文字,首先是问题,然后是正确答案,然后是3个错误答案。你连续读了五行(不需要将它们find
放在一个字符串中),然后打印问题(第一行),然后将三个不正确的答案添加到一些vector<string>
中,然后打乱vector
,然后将正确的答案插入一个随机的位置[0-3],但记住是哪个。从用户那里获得答案,并与你记住的内容进行比较。
然后重复循环。这将是一个非常优雅的解决方案,您可以对此进行扩展。你想让用户跳过一个答案吗?没问题,引入一个bool
标志,它最初是false
,但一旦用户使用该功能,就会变成true
。当用户要求跳过答案时,首先检查此标志,看看用户是否已经使用了此功能。
因为您将使用循环,所以此代码将只添加一次。这很好。如果你意识到你应该更改代码,你将只在一个地方更改它,而不是在20个地方。太棒了。
相关文章:
- Bash 自动完成一个选项,而不运行应用程序
- C++,当函子不是一个选项时,我如何编写带有自定义函数调用的模板化 RAII 包装器?
- 以不同的方式调度 r 值和 l 值,并使用 sfinae 禁用一个选项
- 将字符指针按顺序存储在 map 中 std::map<char*, int> mymap。将其存储为字符或字符串不是一个选项
- clang,linux是否有一个选项可以在链接时更改共享库名称
- boost::p rogram_options:是否可以将一个选项设置为另一个选项的默认值
- 如何防止用户多次选择一个选项
- 带有一个选项卡的 QTabBar 太宽
- GNU Make:如何在列表的每个条目前面添加一个选项
- 如何在QTabWidget中仅将一个选项卡设置为在Qt中不可移动
- 在双括号 QString 之间仅选择一个选项
- 当选择一个选项时,如何执行另一个c++程序
- c++/boost program_options一个选项禁用另一个选项
- Qt名称冲突,但no_keywords不是一个选项
- 用c++编写一个菜单程序,让用户从选项列表中进行选择,如果输入的不是其中一个选项,则重新打印该列表
- 另一个选项是双指针
- 使用-Wpedantic选项编译c++ 11:是否有一个选项可以只禁用关于未命名结构的警告
- CMake -从另一个CMake文件的GUI设置中删除一个选项
- QTabWidget 选项卡在其中一个选项卡中不显示任何内容
- 从另一个选项卡FLTK中开始的线程隐藏小部件