簡體   English   中英

ifstream getline - 將 txt 文件讀取到對象數組,但它只讀取第一行?

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

我正在編寫一個初學者程序,它接收一個包含 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){ check is true,則代碼調用setTriviaQuestion(holder)並增加fiveLineCounter 現在是1

然后接下來檢查if (fiveLineCounter == 1){是否為真,因此代碼調用setAnswer1(holder) (在holder使用相同的行)並增加fiveLineCounter 現在是2

然后接下來檢查if (fiveLineCounter == 2){為真,...

這一直持續到setCorrectAnswer(stoi(holder)) 其中stoi(holder)拋出異常,因為holder的內容(仍然是文件的第一行)無法解析為整數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM