Code:
//
// main.c
// Final Exam
//
// Created by Matt Spadafora on 8/28/14.
// Copyright (c) 2014 Matt Spadafora. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int computeGrade();
int main()
{
char fName[20];
char lName[20];
char buf[100];
char *token;
int q1, q2, q3, q4, mid1, mid2, final,total;
char fname[30];
FILE *ifp, *ofp;
printf("Enter input .txt file name\n");
scanf("%s",fname);
strcat(fname,".txt");
ifp = fopen(fname,"r");
ofp = fopen("output.txt", "w");
if(ifp == NULL)
{
printf("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
if (ofp == NULL)
{
printf("An error has been generated while attempting to open the output file");
fclose(ofp);
exit(1);
}
while (fgets(buf, sizeof(buf), ifp) != NULL)
{
token = strtok(buf, " ");
strcpy(fName, token);
token = strtok(NULL, ",");
strcpy(lName, token);
token = strtok(NULL, ",");
q1 = atoi(token);
token = strtok(NULL, ",");
q2 = atoi(token);
token = strtok(NULL, ",");
q3 = atoi(token);
token = strtok(NULL, ",");
q4 = atoi(token);
token = strtok(NULL, ",");
mid1 = atoi(token);
token = strtok(NULL, ",");
mid2 = atoi(token);
token = strtok(NULL, ",");
final = atoi(token);
printf("%s %s, %d, %d, %d, %d, %d, %d, %d\n", fName,lName,q1,q2,q3,q4,mid1,mid2,final);
}
computeGrade(&q1,&q2,&q3,&q4,&mid1,&mid2,&final);
printf("%d", total);
return 0;
}
int computeGrade(int *qA, int *qB, int *qC, int *qD, int *qE, int *qF, int *qG)
{
int total;
printf("\n\nTHIS IS INSIDE THE PROGRAM!!!!\n");
printf("%d, %d, %d, %d, %d, %d, %d\n",*qA, *qB, *qC, *qD, *qE, *qF, *qG);
total += *qA, *qB, *qC, *qD, *qE, *qF, *qG;
printf("\n%d", total);
return total;
}
The problem I'm having now is how to go about passing the grades to the function computeGrade and then compute the average and return that to the function.