C ++:来自外部文件的琐事

时间:2018-10-30 09:10:09

标签: c++

我有此代码,可以正常工作。我需要包含以“?”分隔的问题和答案在一个外部文本文件中,我需要一些帮助。

现在我有以下问题和答案:

trivia.setQuestion("Who is the patron saint of Ireland?");
        trivia.setAnswer("St. Patrick");
        trivia.setAmount(2);
        triviaList.push_back(trivia);

        trivia.setQuestion("Name Batman’s crime fighting partner?");
        trivia.setAnswer("Robin");
        trivia.setAmount(10);
        triviaList.push_back(trivia);

        trivia.setQuestion("What is the name of Peppa Pig’s brother?");
        trivia.setAnswer("George");
        trivia.setAmount(5);
        triviaList.push_back(trivia);

编辑:我想将问题传递给setQuestion(),并将答案传递给setAnswer()

trivia.text
Who is the patron saint of Ireland? St. Patrick
Name Batman’s crime fighting partner? Robin
What is the name of Peppa Pig’s brother? George

我知道我需要实施此操作:

string line;
cout << "Enter input: ";
getline(cin, line, "?");

当然也可以打开和关闭trivia.text,但是我该如何将问题和答案带入本地?

任何帮助将不胜感激,在此先感谢!

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class TriviaGame {

public:
    TriviaGame();
    TriviaGame(string question, string answer, int amount);
    ~TriviaGame() {};
    string getQuestion();
    void setQuestion(string newQuestion);
    string getAnswer();
    void setAnswer(string newAnswer);
    int getAmount();
    void setAmount(int newAmount);

private:
    string question;
    string answer;
    int amount;
};

TriviaGame::TriviaGame() {
    question = "";
    answer = "";
    amount = 0;
}

TriviaGame::TriviaGame(string question, string answer, int amount) {
    this->question = question;
    this->answer = answer;
    this->amount = amount;
}

string TriviaGame::getQuestion() {
    return question;
}

void TriviaGame::setQuestion(string newQuestion) {
    question = newQuestion;
}

string TriviaGame::getAnswer() {
    return answer;
}

void TriviaGame::setAnswer(string newAnswer) {
    answer = newAnswer;
}

int TriviaGame::getAmount() {
    return amount;
}

void TriviaGame::setAmount(int newAmount) {
    amount = newAmount;
}

    // main function
int main() {
    vector<TriviaGame> triviaList;
    TriviaGame trivia;

    // === Input Stream ===
    fstream inputStream;
    inputStream.open("trivia.txt");
    if(!inputStream.is_open()) {
        cout << "File not found!\n";
        exit(0);
    }
    else {
        string line;

   // while(getline(inputStream, line, "?")){
        while(getline( inputStream, line, '?')) {

trivia.setQuestion(line);
trivia.setAnswer("St. Patrick");
trivia.setAmount(2);
triviaList.push_back(trivia);

trivia.setQuestion(line);
trivia.setAnswer("Robin");
trivia.setAmount(10);
triviaList.push_back(trivia);

trivia.setQuestion(line);
trivia.setAnswer("George");
trivia.setAmount(5);
triviaList.push_back(trivia);
    int winnings = 0;
    cout << "Trivia Game!" << endl;
    for (int i=0; i < triviaList.size(); i++)
    {
        cout << "You have $" << winnings << endl;
        cout << triviaList[i].getQuestion() << endl;
        string answer;
        getline(cin, answer);
        if (answer == triviaList[i].getAnswer())
        {
            cout << "That's right!  You win $" <<
            triviaList[i].getAmount() << endl;
            winnings += triviaList[i].getAmount();
        }
        else
        {
            cout << "Sorry, the correct answer is: " <<
            triviaList[i].getAnswer() << endl;
        }
    }
    cout << "Game over.  Your total winnings are: $" << winnings;

    cout << endl;
}

1 个答案:

答案 0 :(得分:0)

您已经意识到需要做什么:

  

getline(cin, line, "?");

您只需要查看std::getline的详细信息,发现它应该使用char而不是const char*(都不是std::string)作为分隔符。只需将"?"替换为'?'

我准备了一个简短的示例,您如何使用它:

int main() {
    std::istringstream input{"The question? The answer.\n"
                             "Another question? Another answer.\n"
                             "What is 2+2? Definitely 4.\n"};

    std::string question{};
    std::string answer{};

    while(std::getline(input, question, '?') &&
          std::getline(input, answer)) {
        std::cout << "Q: " << question << " A: " << answer << '\n';
    }
}

在上面的示例中,您有两个std::string-一个按住问题,另一个按住答案

不要忘记#include <sstream>的{​​{1}}。如果您不知道std::istringstream是什么,则可以单击上面的链接,或者只是暂时假设它只是一个'硬编码的std::istringstream '。它的行为类似于输入流(就像std::cin一样,但是可以用std::cinconst char*初始化。

要使您的代码使用标准输入,只需删除std::string部分并将std::istringstream替换为input

要使您的代码与文本文件一起使用,请阅读有关std::ifstream1的信息。它的行为非常类似于std::cin(因为它们都是输入流)。创建std::cin对象,将其命名为std::ifstream(或根据您的喜好命名),并像对待input一样对待它。