Turns out getting rid of the weird stuff was not that hard.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Student {
char name[20];
char status[7];
double grade1, grade2, grade3, average;
};
char *getInputFileName();
char *getOutputFileName();
void readStudents(struct Student students[], char *inputFileName);
void writeStudents(struct Student students[], char *outputFileName);
int main()
{
/*Declare and initialize variables*/
struct Student students[5];
char *inputFileName;
char *outputFileName;
inputFileName = getInputFileName();
outputFileName = getOutputFileName();
readStudents(students, inputFileName);
writeStudents(students, outputFileName);
system("pause");
return 0;
}
/* Your code goes here */
char *getInputFileName()
{
char *inputFileName = malloc(255 * sizeof(char));
puts("Please enter input file name");
gets(inputFileName);
return inputFileName;
}
void readStudents(struct Student students[], char *inputFileName)
{
char name[20];
char status[7];
double grade1, grade2, grade3, average;
FILE *inFile;
inFile = fopen(inputFileName, "r");
if (inFile == NULL) {
puts("Error opening output file");
system("pause");
return(-1);
}
for (int i = 0; i < 5; i++)
{
fgets(name, 20, inFile);
char *pos;
if ((pos = strchr(name, '\n')) != NULL)
*pos = '\0';
fgets(status, 7, inFile);
if ((pos = strchr(status, '\n')) != NULL)
*pos = '\0';
fscanf(inFile, "%f ", &grade1);
fscanf(inFile, "%f ", &grade2);
fscanf(inFile, "%f ", &grade3);
average = (grade1 + grade2 + grade3) / 3;
if (average < 70)
printf("Failing");
else if (average > 70)
printf("Passing");
strcpy(students[i].name, name);
strcpy(students[i].status, status);
students[i].grade1 = grade1;
students[i].grade2 = grade2;
students[i].grade3 = grade3;
students[i].average = average;
puts(students[i].name);
puts(students[i].status);
printf("%.2f %.2f %.2f %.2f %.2f\n",
students[i].grade1, students[i].grade2,
students[i].grade3, students[i].average);
}
}
fclose(inFile);
char *getOutputFileName()
{
char *outputFileName = malloc(255 * sizeof(char));
puts("Please enter output file name");
gets(outputFileName);
return outputFileName;
}
void writeStudents(struct Student students[], char *outputFileName)
{
FILE *outFile;
outFile = fopen(outputFileName, "w");
if (outFile == NULL) {
puts("Error opening output file");
system("pause");
return(-1);
}
fputs("Name \tGrade 1 \tGrade 2 \tGrade 3 \tAverage \tStatus\n", outFile);
for (int i = 0; i < 5; i++)
{
fprintf(outFile, "%-10s \t%.2f \t%.2f \t%.2f \t%.2f \t%-10s\n", students[i].name,
students[i].grade1, students[i].grade2, students[i].grade3, students[i].average, students[i].status);
}
/* Exit Program */
system("pause");
return 0;
}
Code:
i686-w64-mingw32-gcc.exe -Wall -g -Wno-unused-local-typedefs -c C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c -o obj/Debug/main.o
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c: In function 'readStudents':
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:67:15: warning: 'return' with a value, in function returning void
return(-1);
^
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:54:6: note: declared here
void readStudents(struct Student students[], char *inputFileName)
^~~~~~~~~~~~
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:81:26: warning: format '%f' expects argument of type 'float *', but argument 3 has type 'double *' [-Wformat=]
fscanf(inFile, "%f ", &grade1);
~^ ~~~~~~~
%lf
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:81:26: warning: format '%f' expects argument of type 'float *', but argument 3 has type 'double *' [-Wformat=]
fscanf(inFile, "%f ", &grade1);
~^ ~~~~~~~
%lf
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:82:26: warning: format '%f' expects argument of type 'float *', but argument 3 has type 'double *' [-Wformat=]
fscanf(inFile, "%f ", &grade2);
~^ ~~~~~~~
%lf
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:82:26: warning: format '%f' expects argument of type 'float *', but argument 3 has type 'double *' [-Wformat=]
fscanf(inFile, "%f ", &grade2);
~^ ~~~~~~~
%lf
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:83:26: warning: format '%f' expects argument of type 'float *', but argument 3 has type 'double *' [-Wformat=]
fscanf(inFile, "%f ", &grade3);
~^ ~~~~~~~
%lf
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:83:26: warning: format '%f' expects argument of type 'float *', but argument 3 has type 'double *' [-Wformat=]
fscanf(inFile, "%f ", &grade3);
~^ ~~~~~~~
%lf
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:100:40: warning: format '%f' expects a matching 'double' argument [-Wformat=]
printf("%.2f %.2f %.2f %.2f %.2f\n",
~~~^
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:100:40: warning: format '%f' expects a matching 'double' argument [-Wformat=]
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c: At top level:
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:109:1: warning: data definition has no type or storage class
fclose(inFile);
^~~~~~
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:109:1: warning: type defaults to 'int' in declaration of 'fclose' [-Wimplicit-int]
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:109:1: warning: parameter names (without types) in function declaration
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c: In function 'writeStudents':
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:133:15: warning: 'return' with a value, in function returning void
return(-1);
^
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:123:6: note: declared here
void writeStudents(struct Student students[], char *outputFileName)
^~~~~~~~~~~~~
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:150:12: warning: 'return' with a value, in function returning void
return 0;
^
C:/Users/stahta01/devel/open_source_code/no_version_control/Test/testc/main.c:123:6: note: declared here
void writeStudents(struct Student students[], char *outputFileName)
^~~~~~~~~~~~~
The code did compile; I was surprised did have a lot of warnings.
Tim S.