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){
检查为真,代码调用 setTriviaQuestion(holder)
并递增 fiveLineCounter
;现在 1
.
然后下一个检查 if (fiveLineCounter == 1){
为真,所以代码调用 setAnswer1(holder)
(与 holder
中的同一行)并递增 fiveLineCounter
;现在 2
.
然后接下来检查if (fiveLineCounter == 2){
为真,...
这一直持续到 setCorrectAnswer(stoi(holder))
。其中 stoi(holder)
抛出异常,因为 holder
的内容(仍然是文件的第一行)无法解析为整数。
我正在编写一个初学者程序,它获取一个包含 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
的内容(仍然是文件的第一行)无法解析为整数。