title: Inputting a file name and having the text read
body: <p>I want the user to enter the name of a file, and if the file exists, print out all the contents of the file. </p>
  
  <p>At the moment the uncommented code, takes a name of a file that the user inputs, for example.     example.txt and prints out most (not the last word?) of the file. I've tried to implement this instead by using string (commented code is attempt) but clearly its incorrect.</p>
  
  <p>I also wondering if i can automatically add .txt to the end of the user input, so that the console could ask - "which subject should we find more information on" user inputs "math" and it will open "math.txt"</p>
  
  <p>Here is what I´ve tried:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;string&gt;
  using namespace std;
  
  int main() {
  
  
      char filename[50];
      //string getcontent;
      ifstream name;
      cin.getline(filename, 50);
      name.open(filename);
  
      if (!name.is_open()) {
          exit(EXIT_FAILURE);
      }
  
      char word[50];
      name &gt;&gt; word;
      while (name.good()) {
          cout &lt;&lt; word &lt;&lt; " ";
          name &gt;&gt; word;
      }
  
      //if (!name.is_open()) {
          //while (! filename).eof())
          //{
              //getline(name, getcontent)
              //cout &lt;&lt; getcontent &lt;&lt; endl;
          //}
  
          //exit(EXIT_FAILURE); //comes from cstdlib
      //}
  
  
  
      //}
  
  
  
      system("pause");
      return 0;
  }
  </code></pre>