Hi all. I have recently been learning C++ and have gained a moderate understanding of how things work. Texts files seem to confuse me though so i was wondering if someone could help me out. :)
Basically I have a text file which looks something like:
**
<Question> What is 4 + 5
<Option 1> 9
<Option 2> 11
<Answer> 9
**
<Question> What is 9*9
<Option 1> 81
<Option 2> 83
<Answer> 81
Bascially I want some code to open this file. Locate the '**' representing the start of a new question, and then output the first 3 lines <question>, <option 1> and <option 2>. The user will then answer the question and it will be compared to <Answer>.
So far my code looks like this:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string search = "**";
string line;
ifstream myfile("example.txt");
if (myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile, line);
if ((line.find(search, 0)) != string::npos)
{
cout << "found " << search<< endl;
// This is where i get stuck.
//How do I get the next lines of the text file
// I.e. <Question>
}
}
myfile.close();
}
else cout << "Unable to open the file." << endl;
system("PAUSE");
return 0;
}
Thanks very much for your time.