I'm trying to get this code to print from a file data.txt which is in my c folder... it's compiling without errors or warnings, anyone see what could be wrong? As soon as I run the program prints nothing and closes (return 0). Doesn't even show me the menu I made.
Code:
/*
My name is Jack Trocinski
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void printStudents( struct datarec *a, int k);
struct datarec {
char name[15];
char ssn[12];
char gender;
double a, q, m, p, f;
char grade;
};
int main()
{
struct datarec temp;
struct datarec x[20];
int j; // used in menu
int k; // used in while loop
FILE *fp; // file pointer
fp = fopen("c:\\data.txt","r");
k = 0;
while( fscanf (fp,"%15s %11s %c %lf %lf %lf %lf",
temp.name,
temp.ssn,
&temp.gender,
&temp.a,
&temp.q,
&temp.m,
&temp.p,
&temp.f )!= EOF ); {
temp.name[14]='\0';
temp.ssn[11]='\0';
x[++k] = temp;
}
do {
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
printf("@ @");
printf("@ 1. Print list of students @");
printf("@ 2. Print list of students with letter grade @");
printf("@ 3. Print list of students with an A grade @");
printf("@ 4. Print list of students with an F grade @");
printf("@ 5. Print list of students sorted by name @");
printf("@ 6. Add student to the class list @");
printf("@ 7. Delete student from class list @");
printf("@ 8. Exit @");
printf("@ @");
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
scanf("%d", &j);
if (j == 1) { printStudents(x, k); }
} while (j != 8);
return 0;
}
void printStudents(struct datarec *a, int k)
{
int i;
for(i=0; i < k; ++i) {
printf( "Name:%-15s SSN:%-11s Gender:%c %4.1f %4.1f %4.1f %4.1f\n",
a[i].name,
a[i].ssn,
a[i].gender,
a[i].a,
a[i].q,
a[i].m,
a[i].p,
a[i].f);
}
}