How to append an endline within a vector string

So in my vector string, my program is reading in the line texts from a Constitution text file, and although it is successfully reading in each line, I am unsure how to separate each line with an end line. For instance, the first line is the Preamble, then under the preamble the text starting with "We the the people.." should be located, however it continues on the same line as Preamble.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  // create function to read in constitution file and also implement reading each line in file
// implement vectors and strings for storing words
//Step 1) be able to read in a line and have an option exit program:
void ReadtheFile(vector<string> &Constitution , int &lineNumber){

// open the file
// line number increases each time there is a line that isn't equal to a space
// create a vector string that inputs all of the lines from constitution text file

ifstream InfileStream;
string fileText = "ConstitutionAndBillOfRights.txt";
string inputLine;

InfileStream.open(fileText.c_str());
// verify that file is open
if(!InfileStream.is_open()){
    cout << "Could not find Constitution text" << endl;
    exit(-1);
}

// input lines from file
while(getline(InfileStream, inputLine)){
//store lines into Constitution vector
if(inputLine.length() == 0 || inputLine[ 0] == ' '){
    continue;
}
int Size = inputLine.size();
if(inputLine.length() > 1){
    Constitution.push_back(inputLine);
    
    lineNumber++; // line number for Constitution Text
}
}
InfileStream.close();
//cout <<  Constitution.at(1); // checking that vector stores in each line correctly
}// end ReadtheFile)

1
2
3
if(inputLine.back() != '\n') { // the back() function returns the last char of the string.
  inputLine += '\n';
}
Last edited on
Thank you for your feedback! I get a compiler error when I use the .back() member function for some reason.
Huh. Are you using c++11?
Hello Fredo25,

It would be much easier if you would provide a sample of the input file so everyone will be using the same information.

Also it helps to provide enough code that can be compiled so people do not have to guess at what you did.

Andy
Topic archived. No new replies allowed.