ifstream getline-将txt文件读取到对象数组,但仅读取第一行?

问题描述

我正在编写一个初学者程序,该程序需要一个包含10个琐事问答的文本文件,读取该文件,将问题和答案放入数组,然后将这些问题用于琐事游戏。

当前,我在将文件读入数组时遇到问题。仅读取文件的第一行。

我是调试新手,但是我尝试使用Vectors重写程序,并且遇到了同样的问题。

这是琐事文件(答案末尾的数字是正确的答案):

The Gettysburg Address
The US Declaration of Independence
The Magna Carta
The US Bill of Rights
2
(2) Who said "A billion dollars isn't worth what it used to be"?
J. Paul Getty
Bill Gates
Warren Buffet
Henry Ford
1
(3) What number does "giga" stand for?
One thousand
One million
One billion
One trillion
3
(4) What number is 1 followed by 100 zeros?
A quintillion
A googol
A moogle
A septaquintillion
2
(5) Which of the planets is closest in size to our moon?
Mercury
Venus
Mars
Jupiter
1
(6) What do you call a group of geese on the ground?
skein
pack
huddle
gaggle
4
(7) What do you call a group of geese in the air?
skein
pack
huddle
gaggle
1
(8) Talk show host Jerry Springer was the mayor of this city.
Chicago
Indianapolis
Cincinnati
Houston
3
(9) On a standard telephone keypad,the letters T,U,and V are matched to what number?
5
6
7
8
4
(10) Crickets hear through this part of their bodies.
Head
Knees
Ears
Tail
2

这是我当前的程序:


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

//Question class
class Question{
private:
    string triviaQuestion;
    string answer1;
    string answer2;
    string answer3;
    string answer4;
    int correctAnswer;  //1,2,3 or 4

public:
    Question();

    //mutator functions
    void setTriviaQuestion(string);
    void setAnswer1(string);
    void setAnswer2(string);
    void setAnswer3(string);
    void setAnswer4(string);
    void setCorrectAnswer(int);

    //accessor functions
    string getTriviaQuestion();
    string getAnswer1();
    string getAnswer2();
    string getAnswer3();
    string getAnswer4();
    int getCorrectAnswer();


};

//Question class  member functions
Question::Question(){
    //initialize member variables
    correctAnswer = 0;
    triviaQuestion = " ";
    answer1 = " ";
    answer2 = " ";
    answer3 = " ";
    answer4 = " ";
}

void Question::setTriviaQuestion(string question){
    triviaQuestion = question;
}

void Question::setAnswer1(string option) {
    answer1 = option;
}

void Question::setAnswer2(string option) {
    answer2 = option;
}

void Question::setAnswer3(string option) {
    answer3 = option;
}

void Question::setAnswer4(string option) {
    answer4 = option;
}

void Question::setCorrectAnswer(int number) {
    correctAnswer = number;
}


string Question::getTriviaQuestion(){
    return triviaQuestion;
}

string Question::getAnswer1() {
    return answer1;
}

string Question::getAnswer2() {
    return answer2;
}

string Question::getAnswer3() {
    return answer3;
}

string Question::getAnswer4() {
    return answer4;
}

int Question::getCorrectAnswer() {
    return correctAnswer;
}



//main function
int main() {
    //variables
    int playerOnescore = 0;
    int playerTwoscore = 0;
    string holder = " ";
    const int ARRAY_SIZE  = 10;
    Question triviaInfo[ARRAY_SIZE];

    //check for a file's existence before opening it
    ifstream dataFile;
    dataFile.open("trivia.txt");
    if (dataFile.fail()){
        //The file does not exist
        cout << "ERROR: Cannot open trivia File.";
    }
    else{
        //the file already exists

        //get the data from the file and put into the Question array
        //for each element of the array
        int fiveLineCounter = 0;
        int arrayCounter = 0;

        while (getline(dataFile,holder)){

            cout << holder << endl; // test to see what's being entered

            if (fiveLineCounter == 0){
                triviaInfo[arrayCounter].setTriviaQuestion(holder);
                fiveLineCounter++;
            }

            if (fiveLineCounter == 1){
                triviaInfo[arrayCounter].setAnswer1(holder);
                fiveLineCounter++;
            }

            if (fiveLineCounter ==2){
                triviaInfo[arrayCounter].setAnswer2(holder);
                fiveLineCounter++;

            }

            if (fiveLineCounter == 3){
                triviaInfo[arrayCounter].setAnswer3(holder);
                fiveLineCounter++;

            }

            if (fiveLineCounter == 4){
                triviaInfo[arrayCounter].setAnswer4(holder);
                fiveLineCounter++;
            }

            if (fiveLineCounter == 5){
                triviaInfo[arrayCounter].setCorrectAnswer(stoi(holder));
                arrayCounter++;
                fiveLineCounter = 0;
            }

        }

    }


    return 0;
}

运行程序时,这是当前输出

(1) What famous document begins: "When in the course of human events..."?

Process finished with exit code 0

非常感谢您提供任何帮助或指导,以解决此问题。 谢谢!

解决方法

fiveLineCounter从零开始。因此if (fiveLineCounter == 0){检查为真,代码调用setTriviaQuestion(holder)并递增fiveLineCounter;现在是1

然后下一个检查if (fiveLineCounter == 1){为真,因此代码调用setAnswer1(holder)(在holder中有同一行)并递增fiveLineCounter;现在是2

然后下一个检查if (fiveLineCounter == 2){为真,...

这一直持续到setCorrectAnswer(stoi(holder))stoi(holder)引发异常,因为holder的内容(仍然是文件的第一行)无法解析为整数。