Search code examples
c++arraysobjectifstreamgetline

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.

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.

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!


Solution

  • fiveLineCounter starts as zero. So if (fiveLineCounter == 0){ check is true, the code calls setTriviaQuestion(holder) and increments fiveLineCounter; it's now 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; it's now 2.

    Then the next check if (fiveLineCounter == 2){ is true, ...

    This continues until 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.