getting entire line to read from a file.

Hello, I have a program I am trying to run that allows me to read in a question from a file and then also read in the answer from a file. I have 5 questions, that have 5 true/false answers. But whenever I try to read in the question it goes crazy and doesn't do what I want. I have a function called "fillQuizFromFile()" that I am using for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void fillQuizFromFile()
{
	ifstream infile;
	string question;
	string answer;
	string line;
	Question test;

	Quiz qz;
	
	
	cout << "\n\nPlease only answer with (True) and (False)." << endl;
	cout << "***Does not require use of parenthesis***"<< endl << endl;

	infile.open("QandA.txt");

	if (infile.fail())
		cout << "Error opening file." << endl;
	else
	{
		cout << "Welcome to the Quiz Show!" << endl;
		cout << "-------------------------" << endl << endl;

		cout << "\t\tOnly answer with (True) and (False)." << endl;
		cout << "\t\t***Do not use parenthesis***"<< endl << endl;
	
		while(infile >> question >> answer)
		{		
			getline(cin, line);
			test.setQ(question);
			test.setA(answer);
	   qz.add(test);
	}

	qz.giveQuiz();

	cout << endl << endl;

	cout << "\tResults" << endl;
	cout << "\t-------" << endl << endl;
	cout << "       Correct: " << setw(2) << qz.getNumCorrect() << endl;
	cout << "       Incorrect: " << qz.getNumIncorrect() << endl << endl;

	cout << "Your final score was a: " << qz.finalScore() << "%" << endl;

	infile.close();
	}
}



And this is the .txt file "QandA.txt" that I am using with it.

1
2
3
4
5
6
7
8
9
10
This is question 1?
True
This is question 2?
True
This is question 3?
True
This is question 4?
False
This is question 6?
False



I would like for the output to look like this...

1
2
3
4
5
This is question 1?
(cin answer...)
This is question 2?
(cin answer...)
etc....


Any ideas?
I am stuck and can't figure out where to go. I used getlines and cin.ignores(), but nothing seems to work.
This might help as well.. This is the header file that holds the member functions for setQ and setA....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Question
{
private:
	string question;
	string answer;
public:
	Question();
	Question(string query, string result);
	void setQ(string quest);
	void setA(string answr);
	string getQuestion();
	string getAnswer();
	bool answerCorrect(string candidateAnswer);

	void displayQuestion();
};

Question::Question()
{
	question = "unknown";
	answer = "answer";
}

Question::Question(string query, string result)
{
	question = query;
	answer = result;
}

void Question::setQ(string quest)
{ question = quest; }

void Question::setA(string answr)
{ answer = answr;  }

string Question::getQuestion()
{  return question; }

string Question::getAnswer()
{  return answer;  }

bool Question::answerCorrect(string candidateAnswer)
{
	if (candidateAnswer == answer)
		return true;
	else
		return false;
}

void Question::displayQuestion()
{
	cout << question << endl;
	cout << answer << endl;
}
The >> operator only reads the first word.
Apparently you want to read the whole line. In taht case you should use getline(infile,question).
Topic archived. No new replies allowed.