Reading multiple choice questions from a text file

Hello,

I am trying to write a program that will read multiple choice questions from a text file that contains a bank of 5 questions. Every time the user opens the program, it will give them the questions in a random order. After the user is done, it should give the user a final score.

Below is my code. I am having issue formating the output the way I want it to display. I would like it to display exactly how I have it in my text file.

In my text file the format is like this:

1. What is the answer to 2+3?
a. 6
b. 9
c. 5
d. 2


When i run the program, it only read the 1st line.

Here is my text file a.
https://drive.google.com/open?id=0BwMYvDYYBU_oNG9pUkUtalNibkk

Please help. Thanks in advance.

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
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream inputFile;
	string question1;

	inputFile.open("test.txt");
	if (!inputFile)
		cout << "Error opening data file\n";
	else
	{
		getline(inputFile, question1);

		inputFile.close();



		cout << question1;
	}
	
	system("pause");

	return 0;

}
Last edited on
You have to use loops for getting the whole input.... depends on how you are implementing it

PS: I don't see a random function, so how do you chose the question?

PPS: Please also show us the text file you are using
I wanted to get this portion down before going to the random function.

I've added a link to my text file.
My idea is to create a struct for the actual quiz entry, implement reading them from the file and store them in a vector so you can easily get a random entry.

1
2
3
4
5
6
7
8
9
10
11
12
13
struct QuizEntry
{
  std::string question;
  int answers[4];
};

std::istream& operator>>(std::istream& is, QuizEntry& entry)
{
  // code to read
  return is;
}

std::vector<QuizEntry> Quiz;


BTW. Where do you want to store the right answer?
We have not learned struct or vectors in our class. So far we are at array and looping.

Is it possible to code with array and looping..

I was planning on having the answer right below the choices, and after the user enter their choice I would compare it with that line.

for example:

1. What is the answer to 2+3?
a. 6
b. 9
c. 5
d. 2

c

if (ans1 == ??)

score += 5;

?? not sure how to compare the users answer to the line with the correct answer..

Topic archived. No new replies allowed.