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.

you call getline() again to get the next line(s)

Assuming that your code works fine till this line(if not hoping that you will successfully solve the problems :) )you can do this :

if ((line.find(search, 0)) != string::npos)
{
      cout << "found " << search<< endl;                                               
      //Here at this point you know that the next three lines are the question and the options so you can do something as
      getline(myFile,line);
      cout<<line;
      getline(myFile,line);
      cout<<line;
      getline(myFile,line);
      cout<<line;
      //By this time you have shown the user the question and also the options so fetch his input do a getline again and compare users answers with the answer provided in the text file as the next getline returns the fourth line of the question.

}

Thanks a bunch. I think i have it sorted now.

Ok I'm stuck again :(

Basically I want to display the question. Then display choice 1 and choice 2. I then want the program to pause, grab the users input and then compare to the answer. If the answer and the user input are the same then i want the user to gain one point. If not then i want the user's score to remain the same. I've tried this already but when i try and compare i get various errors. :S Please could someone point me in the correct direction. Thanks.

Here's the code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void disp_ques(char* file);


int main () {
 
 char file[100] = "example.txt";
 
 ifstream myfile(file);
 if(myfile.is_open())
 {
                     disp_ques(file); 
                     myfile.close();        
 } else cout << "an error has occured"; 
 
 
 system("PAUSE");
 return 0;   
}


void disp_ques(char* file)
{
     string line;
    
     ifstream myfile(file);
     getline(myfile, line);
     while(!myfile.eof()){
                         // What do i need to add here to Display the questions 
                         // and stop before the answer is displayed.
                         // Also need to grab users input so it can be compared to answer

                          
     }    
}

Ok I'm stuck again :(

Basically I want to display the question. Then display choice 1 and choice 2. I then want the program to pause, grab the users input and then compare to the answer. If the answer and the user input are the same then i want the user to gain one point. If not then i want the user's score to remain the same. I've tried this already but when i try and compare i get various errors. :S Please could someone point me in the correct direction. Thanks.

Here's the code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void disp_ques(char* file);


int main () {
 
 char file[100] = "example.txt";
 
 ifstream myfile(file);
 if(myfile.is_open())
 {
                     disp_ques(file); 
                     myfile.close();        
 } else cout << "an error has occured"; 
 
 
 system("PAUSE");
 return 0;   
}


void disp_ques(char* file)
{
     string line;
    
     ifstream myfile(file);
     getline(myfile, line);
     while(!myfile.eof()){
                         // What do i need to add here to Display the questions 
                         // and stop before the answer is displayed.
                         // Also need to grab users input so it can be compared to answer

                          
     }    
}

Refer to my previous post I have answered this question of yours there itself. Replace the last comment with a cin to pause and get input from the user and once the input is got match it with the answer and then display it to the user. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.