-1

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?

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • Read lines with `getline(file, pointer[index]); ` and add a new line at each line output: `cout << pointer[index] << '\n';` – René Richter Nov 22 '14 at 16:40
  • Thank your instructor for allowing you to use `std::string`. We need more instructors like this. – Thomas Matthews Nov 22 '14 at 16:57
  • You might also want to implement a copy constructor and copy assignment operator (Rule of Three). For reference: http://stackoverflow.com/q/4172722/1025391 – moooeeeep Nov 22 '14 at 17:19

2 Answers2

0

Just add endl (end line) to your output.

void textLines::printArray(){

    for (int index = 0; index < stringsize; index++) {
        cout << pointer[index] << endl;
    }

}
hello_world
  • 442
  • 3
  • 7
0

Modify your code in following lines: file >> pointer[index]; to getline(file, pointer[index]; and cout << pointer[index]; to cout << pointer[index] << '\n'; Why? getline() reads one line, not just one word, '\n' provides a new line on output.

Please reconsider the value of stringsize, since you print out garbage if your file contains less lines. Much more "C++ style" would be to have a member vector<string> lines; with

string input;
while(getline(file, input) lines.push_pack(input);

for input and

for (size_type i = 0; i < lines.size(); ++i)
  cout << lines[i] << '\n';

for output. Your class with naked pointers in the current form is prone to leak memory when used with assignment operator (see Rule of Three).

René Richter
  • 3,839
  • 3
  • 34
  • 42