/* A program which, for given integer N, outputs all the even integers 
 * from 1 to N to a file called "even.txt" and all the odd integers from 1 
 * to N to a file called "odd.txt"
 */

/* We use the "fstream.h" library, which is a C++ library for file 
 * input/output; this means we need to use the g++ compiler. (There are 
 * equivalent C commands, but they're a bit harder to use.)
 */

#include <fstream.h>
#include <stdio.h>

int main()
{
  int i, N;

  /* The "ofstream" command associates a filename (like "even.txt") with a 
   * variable name (like "file1") that the program will understand
   */

  ofstream file1("even.txt");
  ofstream file2("odd.txt");

  /* Output to the screen does not affect the files
   */

  printf("Input a positive integer: ");
  scanf("%d", &N);

  for (i = 1; i <= N; i++)
  {
    if (i%2 == 0)
    {
      /* To output to a file, give the name of the ofstream variable, 
       * followed by variables and/or strings
       */
 
      file1 << i << "\n";
    }
    else
    {
      file2 << i << "\n";
    }
  }

  printf("Done!\n");

  /* Close the files to make sure nothing gets left in the computer's 
   * "buffer"
   */

  file1.close();
  file2.close();

  return(0);
}

