I've written the code to determine if a year is a leap year or not, reading from a file, and printing to a file, and telling how many years were checked. I'm having trouble figuring out how to convert it to utilizing functions in the program, for the actual calculations and the printing of the output. Any help here would be appreciated!

#include <stdio.h>
main()
{
    int year, totalYears, e;
    FILE*InFile, *OutFile;
    InFile = fopen("data3.txt","r");
    OutFile = fopen("out3.txt","w");
    fprintf(OutFile, "Leap Year Check\n");
    fprintf(OutFile, " Year  Result\n");
    totalYears = 0;
    e=fscanf(InFile,"%d",&year);

    /*
    Calculate whether a year is a leap year or not.
    */
    while (e==1)
    if(year%400 == 0 || (year%100 != 0 && year%4 == 0))
    {
        fprintf(OutFile, " %d  Leap Year\n",year);
        e=fscanf(InFile,"%d",&year);
        totalYears = totalYears + 1;
    }
    else
    {
        fprintf(OutFile, " %d  Not Leap Year\n",year);
        e=fscanf(InFile,"%d",&year);
        totalYears =  totalYears + 1;
    }
        fprintf(OutFile, "%d years were checked",totalYears);
    fclose(InFile);
    fclose(OutFile);
}

The only function I would add, is a calculate() function, that would start with the while loop, and extend down to just before the fclose(InFile), line of code. Every variable you use in the while loop, needs to be brought down into the new function, as a parameter. It doesn't need to return any value, so a void function would be fine.

The printing is all tightly integrated into the calculations, so I wouldn't try to make a separate function for that.

In C, it's always int main(void) or int main(argc, *argv[]) , never just main(), and always with a return 0 at the very end of main().

Would you be able to get me a little more information about how to do that? I'm just getting into the functions, and am having a lot of trouble getting to that point.

The normal template for a simple C program is:

#include files listed    //no semi-colons at the end of these lines of code

#defines SIZE 20  //no semi-colons at the end of these lines SIZE in the code (everywhere), will be replaced by the number 20. 

function prototypes go here. These can be exactly the same
as the first line of the function (copy and paste), except,
they will have an added semi-colon at the end of them.

void function1(int number, char choice, double salary);
(could be shortened to just (int, char, double) parameters)

other global variables if needed (use very sparingly)

int main(void) {
  //declare your local variables, for main() here
  int number;
  char choice;
  double salary;

  //begin your program's logic code here
  //and/or 
  //call any other functions you might have

  function1(number, choice, salary); //call to function1 

  printf("\n\n\t\t\t    press enter when ready"); //if needed
  getchar(); //if needed to hold the console window open
  return 0;  //normal run signal to the OS
}
void function1(int number, char choice, double salary) 
{  //list of the local variables for function1 go here
   
   //logic goes here

   //this is a void function, so nothing to return to main
   //if a return was needed, it would go here (last line of code)
}

Note: in function1(), I have COPIES of number, choice, and salary,
NOT the original variables in main. If I want any changes to be
permanent, I need to send the address of these variables (using &,
ie:
function1(&number, &choice, &salary), and have

function1(int *number, char *choice, double *salary)

receive these variables, as pointers, not regular data types.

If you look around the forum, you'll see lots of code. Using indentation, and posting it surrounded by the code tags (press on icon at the top of the editing window to get a pair of them, then paste your code, between them), is important for readability.[CODE ] icon at the top of the editing window to get a pair of them, then paste your code, between them), is important for readability.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.