如何防止用户多次选择一个选项

时间:2016-06-12 20:30:26

标签: c++

我写了一个非常简单的测验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 millionaire\n";
    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 Losers\nB. Blackburn Rovers\nC. Blackburn Lovers\nD. BlackburnWanderers\n";
        }
        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. Fridge\nB. Bank\nC. Market\nD. Shoe\n";
        }
        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. Bassey\nB. Kwesi\nC. Abiodun\nD. Ejima\n";
        }
        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. Inferno\nB. Domino\nC. Stiletto\nD. Tornado\n";
        }
        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 wife\nB. Bury a dead parent\nC. Have thanksgiving in church\nD. Accept gifts or favour in kind\n";
        }
        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. Tenacity\nB. Verifiability\nC. Hereditary\nD. Validation\n";
        }
        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. Paris\nB. Copenhagen\nC. New York\nD. Madrid\n";
        }
        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. Mumbai\nB. Beijing\nC. Rio de Janeiro\nD. Seville\n";
        }
        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 sound\nB. A speed greater than that of sound\nC. A speed equal to that of light\nD. A speed greater than that of light\n";
        }
        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. Nephrons\nB. Nerves\nC. Ligaments\nD. Stitches\n";
        }
        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. Swimmer\nB. Referee\nC. Football Fan\nD. Judoka\n";
        }
        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. France\nB. United States\nC. Germany\nD. India\n";
        }
        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 Eastwood\nB. Oliver Stone\nC. Peter Jackson\nD. Morgan Freeman\n";
        }
        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. Lebanon\nB. Columbia\nC. Japan\nD. Eritrea\n";
        }
        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. Literature\nB. Economics\nC. Peace\nD. Medicine\n";
            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)

2 个答案:

答案 0 :(得分:2)

声明一个名为“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.");
}

答案 1 :(得分:0)

首先,你必须压缩&#34;香肠&#34;代码进入循环。唯一不同的是可能的答案,以及哪一个是正确答案。我建议你把它放在文件中,所以文件看起来像这样:

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>,然后shuffle vector,然后将正确答案插入随机位置[0-3],但请记住它是什么。从用户那里获得答案并与您记住的内容进行比较。

然后重复循环。这将是一个非常优雅的解决方案,您可以扩展。您希望用户能够跳过一个答案吗?没问题,首先引入一个bool标记false,但一旦用户使用该功能,则转为true。当用户要求跳过答案时,首先检查此标志,以查看用户是否已经使用此功能。

因为您将使用循环,所以此代码只会添加一次。这很好。如果您意识到您应该更改代码,那么您只需要在一个地方更改代码,而不是在20代。但这真是太棒了。