For my homework lab I'm supposed to:
Define a class called textLines that will be used to store a list of lines of text (each line can be specified as a string or a c-string, whichever you prefer).
Use a dynamic array to store the list.
In addition, you should have a private data member that specifies the length of the list.
Create a constructor that takes a file name as parameter, and fills up the list with lines from the file.
There's more but those are functions and not relevant to the question.
My code so far:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// Define class called textLines (used to store list of lines)
class textLines
{
public:
// Main Constructor
textLines(ifstream& myfile1){
pointer = new string[stringsize];
if (myfile1.fail()) {
cout << "File failed to open.\n";
exit(1);
}
else
for (int index = 0; index < stringsize; index++) {
myfile1 >> pointer[index];
}
}
// Constructor that takes an integer parameter that sets the size of an empty list.
textLines(int){
pointer = new string[0];
}
// Deconstructor
~textLines(){
delete[] pointer;
}
void printArray();
private:
ifstream infile;
ofstream outfile;
static int stringsize;
string* pointer;
};
// Begin Main Function
int main(){
string myfile = "Lab3Text.txt";
ifstream infile(myfile);
textLines text(infile);
text.printArray();
return 0;
}
// End Main
int textLines::stringsize = 1000;
void textLines::printArray(){
for (int index = 0; index < stringsize; index++) {
cout << pointer[index];
}
}
And this is what my text file looks like:
Hello World
Hello World
Hello World
My output comes out like this, however:
Output: HelloWorldHelloWorldHelloWorld
What's a simple fix that I can make my output appear more like my text file, in rows?