title: How to take input file, reverse word order, write to output file in C++
body: <blockquote>
  <p>Take the contents of an input text file and write them in reverse order of words to an output file.  Your program can ignore line breaks.  You will need to use arrays.</p>
  </blockquote>
  <p>(unnecessary crap removed)  Aaaaah, panic, please help!</p>
  <p>EDIT:  What I have so far.  Still lost on how to reverse word order now.</p>
  <pre><code>//Kristen Korz
  //CIS 22A
  //This program reads an input file and writes the words in reverse order to an output file.
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  int main()
  {
      //create and link input...
      ifstream inputFile;
      inputFile.open(&quot;input.txt&quot;);
      //...and output files
      ofstream outputFile;
      outputFile.open(&quot;output.txt&quot;);
  
      //error message for file open fail
      if (inputFile.fail())
          cout &lt;&lt; &quot;Error opening the file.\n&quot;;
  
      //constant for max size
      const int MAXSIZE = 1024;
      //string array and temporary-use string
      string words[MAXSIZE];
      string str;
      //read words from input file
      for (int i = 0; (inputFile &gt;&gt; str) &amp;&amp; (i &lt; MAXSIZE); ++i)
      {
          words[i] = str;
      }
      inputFile.close();
      
      //for showing if read correctly
      cout &lt;&lt; endl;
      for (int i = 0; i &lt; MAXSIZE; ++i)
          cout &lt;&lt; words[i] &lt;&lt; endl;
  
  
  
  
  
      system(&quot;pause&quot;);
      return 0;
  }
  </code></pre>
  <p>What I've got successfully reads the input file word for word.  I can figure out everything except how to reverse the word order for then writing to output.txt
  This is our first program reversing the order of things, yes.</p>
  <h2>EDIT 2:</h2>
  <p>Okay, so the best I can guess is this:</p>
  <pre><code>    //constant for max size
      const int MAXSIZE = 1024;
      //string array and temporary-use string
      string words[MAXSIZE];
      string str;                 //note: variables will be used for output loops too
  
      //read words from input file
      for (int i = 0; (inputFile &gt;&gt; str) &amp;&amp; (i &lt; MAXSIZE); ++i)
      {
          words[i] = str;
      }
      inputFile.close();
      
      //for showing if read correctly
      cout &lt;&lt; endl;
      for (int i = 0; i &lt; MAXSIZE; ++i)
          cout &lt;&lt; words[i] &lt;&lt; endl;
  
      //for writing in reverse word order to output file
      for (int i = MAXSIZE-1; (outputFile &lt;&lt; str) &amp;&amp; (i &gt;= 0); --i)
      {
          words[i] = str;
      }
      outputFile.close();
  
      //for showing if written correctly
      for (int i= MAXSIZE-1; i &gt;= 0; --i)
      {
          cout &lt;&lt; words[i] &lt;&lt; endl;
      }
  </code></pre>
  <p>The input section works fine.  Output just repeats last word of input for each iteration.</p>
  <h2>EDIT 3:</h2>
  <p>Just kidding, everything except actual writing of output file works.  Output in the terminal is corrects by getting rid of the &quot;-1&quot; after <code>MAXSIZE</code> in the initialization.  Adjusting the code that writes the file in a similar way does not solve the repeating &quot;works.&quot; (final word of input file) written to output.txt</p>
  <h2>EDIT 4:</h2>
  <p>Relevant code is now:</p>
  <pre><code>    //constant for max size
      const int MAXSIZE = 1024;
      //string array and temporary-use string
      string words[MAXSIZE];
      string str;                 //note: variables will be used for output loops too
  
      //read words from input file
      for (int i = 0; (inputFile &gt;&gt; str) &amp;&amp; (i &lt; MAXSIZE); ++i)
      {
          words[i] = str;
      }
      inputFile.close();
      
      //for showing if read correctly
      cout &lt;&lt; endl;
      for (int i = 0; i &lt; MAXSIZE; ++i)
          cout &lt;&lt; words[i] &lt;&lt; &quot; &quot;;
  
      //for writing in reverse word order to output file
      for (int i = MAXSIZE; (outputFile &lt;&lt; str) &amp;&amp; (i &gt;= 0); --i)
      {
          words[i] = str;
      }
      outputFile.close();
  
      //for showing if written correctly
      for (int i = MAXSIZE; i &gt;= 0; --i)
      {
          cout &lt;&lt; words[i] &lt;&lt; &quot; &quot;;
      }
  </code></pre>
  <p>If I change <code>i&gt;=0</code> to <code>i=0</code> (which I mistakenly typed first try) in <code>for (int i = MAXSIZE; (outputFile &lt;&lt; str) &amp;&amp; (i &gt;= 0); --i)</code>, then <code>cout</code> in terminal is perfect.  Can't figure out how to get output file to not be repeated &quot;works.&quot; Why is it doing that?  Note: output to terminal optional, so I don't really care why that wants <code>i</code> assigned to 0 in previous for loop in terms of being able to finish the assignment</p>