title: Reading a text file and saving every line as a string (C++)
body: <p>I have a little question. I'm currently doing a school assignment in c++ and the task is to have something similar to a small library, where the user can ask to look at a book and then the program will print out the release year, author, how many pages etc etc. The assignment is focused on vectors and arrays, but I thought a smart way of doing it could be to have the release years in a text file and then save those years in an array. When I first it, everything was saved in characters, (meaning "1","8","8","5"), when I'd actually like it to save every line in the text document as a string in the array, or something similar (like this: "1885",). I couldn't really figure out how to split them up into strings then. I then talked a bit to a friend and this is where I am with my code now, it's not really working and at the moment I have no idea how I am supposed to solve it. <em>Main problem is I don't know how to read and save every line in the text document as a string</em>, however I am grateful for any help that would make me be able to print out a single year from the text document, in any other way.</p>
  
  <p>This is what my code looks like:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;istream&gt;
  
  using namespace std;
  
  void book();
  void readFile(int input);
  void oddEven();
  void stringLiner();
  void factorial();
  int main()
  {
  
  int input;
  while (input != 0)
  {
  cout &lt;&lt; "Hello. Welcome to the first, truly big, assignment in this programming course." &lt;&lt; endl;
  cout &lt;&lt; "Which part do you wish to access?" &lt;&lt; endl;
  cout &lt;&lt; "1. Book program" &lt;&lt; endl;
  cout &lt;&lt; "2. 2 arrays - One EVEN ~ One ODD" &lt;&lt; endl;
  cout &lt;&lt; "3. The one at a time string" &lt;&lt; endl;
  cout &lt;&lt; "4. Factorial array" &lt;&lt; endl;
  cout &lt;&lt; "0. Exit " &lt;&lt; endl;
  cin &gt;&gt; input;
  switch (input)
  {
      case 1:
      book();
      break;
  
  }
  
  
  }
  }
  
  void book() //This is the function used to do the book thing
  {
  cout &lt;&lt; string( 100, '\n' );
  
  int input;
  
  string year[5] = {"1883"/*Treasure Island*/ }; //Array for the years the  books were written
  string author[5] = {"Robert Louis Stevenson"/*Treasure Island*/, "yollo"}; //Array for the authors
  string pages[5] =  {"304"/*Treasure Island*/,"420" }; //Array for the number of pages
  string books[5] = {"Treasure Island", "Swagolo" }; //Array for the name of the books
  cout &lt;&lt; "You have chosen to look at books." &lt;&lt; endl;
  cout &lt;&lt; "These are the books in the library. Pick one to see what year it was written in, what author wrote it and how many pages it contains. " &lt;&lt; endl;
  cout &lt;&lt; "These are the books in the library: " &lt;&lt; endl;
  for (int i = 0; i&lt;5; i++)   //Loop to display all the books + what number to press to access them.
  {
      cout &lt;&lt; i+1 &lt;&lt; " " &lt;&lt; books[i] &lt;&lt; endl;
  };
  cout &lt;&lt; "Please type a number to look at that book. " &lt;&lt; endl; 
  cin &gt;&gt; input;
  int TresIsl = input-1;
  switch (input)  //Switch case to chose which book to look at.
  {
      case 1: //Info about Treasure Island
  
      cout &lt;&lt; "This is " &lt;&lt; books[TresIsl] &lt;&lt; " and this is some info. " &lt;&lt; endl &lt;&lt; endl;
      cout &lt;&lt; books[TresIsl] &lt;&lt; " was released in " ;
      readFile(input);
      cout &lt;&lt; " and it was written by " &lt;&lt; author[TresIsl] &lt;&lt; ". ";
      cout &lt;&lt; "This book contains " &lt;&lt; pages[TresIsl] &lt;&lt; " pages. " &lt;&lt; endl;
      break;
  
      case 2:
      cout &lt;&lt; "This is " &lt;&lt; books[TresIsl] &lt;&lt; " and this is some info. " &lt;&lt; endl &lt;&lt; endl;
      cout &lt;&lt; books[TresIsl] &lt;&lt; " was released in " ;
      readFile(input);
      cout &lt;&lt; " and it was written by " &lt;&lt; author[TresIsl] &lt;&lt; ". ";
      cout &lt;&lt; "This book contains " &lt;&lt; pages[TresIsl] &lt;&lt; " pages. " &lt;&lt; endl;
      break;
  
  
  }
  }
  
  void readFile(int input)
  {
  ifstream file("year.txt");
  int numlines = 0;
  int numMaxLines = 5;
  vector&lt;string&gt; lines (numMaxLines);
  while(numlines &lt; numMaxLines &amp;&amp; !file.eof())
  {
      getline(file, lines);
      numlines++;
  }
  cout &lt;&lt; lines[input];
  
  }
  </code></pre>
  
  <p>The other void functions are for other tasks in this assignment which I didn't include now, I just copy pasted the code where they were included. Also please don't mind the slightly childish stuff in the code.</p>
  
  <p>Also I am very sorry if this breaks any rules for the forum or something similar. I tried to find another topic like this for c++, but I couldn't find anything helpful.</p>