/***************************
 *Jessica Qiu
 *Assignment 4
 *CPSC 101 - Section 001
 *Due: December 2, 2015
***************************/
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   FILE *inPtr, *outPtr;
   char c;
   int wordCount = 0;
   int sentenceCount, num;

   inPtr = fopen("in.txt", "r");
   outPtr = fopen("out.txt", "w");

   if(argv[1] == NULL)
   {
      fprintf(stderr, "Cannot open file for reading.\n");
      return 1;
   }

   if(argv[2] == NULL)
   {
      fprintf(stderr, "Cannot open file for reading.\n");
      return 2;
   }

   fprintf(outPtr, "Below is the text that, for each character, the case \n");
   fprintf(outPtr, "that has not been changed. There are also integers \n");
   fprintf(outPtr, "included in this text.  The integer value has not \n");
   fprintf(outPtr, "been altered.\n");
   fprintf(outPtr, "------------------------------------------------------\n");

   //changes the letters to either uppercase or lowercase in the file
   while(!feof(inPtr)) 
   {
      c = fgetc(inPtr);
 
      if(c >= 'a' && c <= 'z')
      {
	 c = make_upper(c);
      }
      else 
      {
         c = make_lower(c);
      }

      fputc(c, outPtr);
   }
    
  
   fprintf(outPtr,"------------------------------------------------------\n");
    rewind(inPtr);
   //counts the words
   wordCount = countWords(inPtr);
   fprintf(outPtr, "Word Count: %d\n", wordCount);
    rewind(inPtr);
   //counts the sentences
   sentenceCount = CountSentences(inPtr);
   fprintf(outPtr, "Sentence Count: %d\n", sentenceCount);   

   fprintf(outPtr, "------------------------------------------------------\n"); 

   //finds the integers that are in the file
   fprintf(outPtr, "Below is a list of the integers scattered in the file:\n");
    rewind(inPtr);
   findNumber(inPtr, outPtr);

   //adds up all the integers
   sum(inPtr, outPtr);
   
   //averages all the integers
   average(inPtr, outPtr);

   fprintf(outPtr, "------------------------------------------------------\n"); 

   fclose(inPtr);
   fclose(outPtr);

   return 0;
}
