Hello, I joined here desperately asking for your help
I have an assignment about creating a program that loads a text file given.
the text file contains many lines and those lines are questions and answers
it is on this sequence
question
answer
question
answer
and so on

so we basically have to:
1- load the text file
2- set it in a 2-D array
3- output certain lines from the array as questions which means I have to jump the answer line
4- read in answers
4- finally, we have to create a text file based on those prompts and the entire process using c++

I am not asking for you guys to do the assignment for me, that is wrong
but I just want to know how to, like what are the commands for those aims as they haven't given us the heads up for it

it is much appreciated, thanks in advance.

Recommended Answers

All 3 Replies

Well, first up, try to read contents of the file into whatever data structure you are required to use.

File I/O

this isn't that difficult.. it's pretty straight-forward file i/o. which concept is giving you trouble?

this is my code which is working just fine, but the only probelm I'm facing right now is that when the I ask the user to if they want to continue or not (Do while loop)
nothing happens, even when the condition is met, the program doesn't exit the loop

and I have no idea how to make that into a 2D array

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

using namespace std;
ifstream myfile;
ofstream alsomine;
int main()
{

	string answers;
	string name;
	string command;
	string x;
	string yn;
	int loadtest = 0; 
	int correct, wrong;
	correct = 0;
	wrong = 0;

	cout << " Please Enter Your Name: ";
	getline(cin, name);
	cout<< "\n Hello " << name << ". This program tests your knowledge in your chosen category.\n Use commands (e.g. LOAD, PLAY, LIST, FINISH, HELP) to control progresses of the test. ";

		while (command!= "FINISH")
		{
			cout << "\n Please Enter A command: ";
			getline(cin, command);
			if (command== "LOAD british_primeministers.txt")
			{
				myfile.open("british_primeministers.txt");
				if(myfile.good())
				{
					loadtest++;
						cout << " File is Loaded\n ";
				}
				else
				{
					cout << " There was an error loading your file\n ";
				}
			}
			else if (command== "PLAY")
			{
				if (loadtest== 1)
				{
					getline(myfile, x);
					cout << " \n" << x << endl;
					cout << " Number of Entries = 77 ";
					getline(myfile, x);
					cout << "\n " << x << endl;

					
					
						while (!myfile.eof()) 
						{
							do
							{
							getline(myfile, x);
							cout << x;
							getline(myfile, x);
							cout << "\n ";
							getline(cin, answers);
							if (answers== x)
							{
								cout << " Well Done " << endl;
								correct++;
							}
							else
							{
								cout << " Wrong answer, correct answer is: " << x << endl;
								wrong++;
							}
						
							cout << " Would you like to continue? Y/N ";
							cin >> yn;
							}while ((yn != "N") && (yn!="n"));
							
							
						}// while eof
					
				}//load test
				else 
				{
					cout << " Error, the file has not been loaded ";
				}
			}//commands loop
		}// while comman not finish end

						


			


	
	









	system ("PAUSE");
	return 0;
}
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.