简体   繁体   English

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

[英]ifstream getline - Reading txt File to an array of objects, but it's only reading the first line?

I'm writing a beginner program that takes a text file of 10 trivia questions and answers, reads the file, puts the questions and answers into an array, and then uses the questions for a trivia game.我正在编写一个初学者程序,它接收一个包含 10 个琐事问题和答案的文本文件,读取文件,将问题和答案放入一个数组中,然后将这些问题用于琐事游戏。

Currently, I'm having an issue reading the file into the array.目前,我在将文件读入数组时遇到问题。 Only the first line of the file is being read.仅读取文件的第一行。

I'm new to debugging, but I tried to rewrite the program with Vectors and had the same issue.我是调试新手,但我尝试用 Vectors 重写程序并遇到了同样的问题。

Here is the trivia file (the number at the end of the answers is the correct answer):这是琐事文件(答案末尾的数字是正确答案):

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

Here is my program currently:这是我目前的程序:


#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;
}

When I run the program, this is the current output:当我运行程序时,这是当前输出:

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

Process finished with exit code 0

Would really appreciate any help or pointers on how to fix this.非常感谢有关如何解决此问题的任何帮助或指示。 Thank you!谢谢!

fiveLineCounter starts as zero. fiveLineCounter从零开始。 So if (fiveLineCounter == 0){ check is true, the code calls setTriviaQuestion(holder) and increments fiveLineCounter ;因此, if (fiveLineCounter == 0){ check is true,则代码调用setTriviaQuestion(holder)并增加fiveLineCounter it's now 1 .现在是1

Then the next check if (fiveLineCounter == 1){ is true, so the code calls setAnswer1(holder) (with the same line in holder ) and and increments fiveLineCounter ;然后接下来检查if (fiveLineCounter == 1){是否为真,因此代码调用setAnswer1(holder) (在holder使用相同的行)并增加fiveLineCounter it's now 2 .现在是2

Then the next check if (fiveLineCounter == 2){ is true, ...然后接下来检查if (fiveLineCounter == 2){为真,...

This continues until setCorrectAnswer(stoi(holder)) .这一直持续到setCorrectAnswer(stoi(holder)) Whereby stoi(holder) throws an exception, because the contents of holder (still the first line of the file) can't be parsed as an integer.其中stoi(holder)抛出异常,因为holder的内容(仍然是文件的第一行)无法解析为整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM