I made this program and got it to compile without any errors but it doesn't run as expected. If I enter 2 I will get to see all of the student information for each individual. Once I get back to the menu selection and press 2 again I don't see any student information. How can I make it so that every time I press 2 at the menu I get to see all student information?
Code:#include <stdio.h> #include <string.h> int main() { FILE *pAppend; FILE *pRead; char fName[21]; char lName[21]; char id[15]; float gpa=0; int iSelection=0; pAppend = fopen("StudentInformation.dat", "a"); pRead = fopen("StudentInformation.dat", "r"); while(iSelection!=3) { printf("\nStudent Information Exercise\n\n"); printf("Select an option\n"); printf("1)\tEnter student information\n"); printf("2)\tView current student information on file\n"); printf("3)\tExit student information exercise\n\n"); scanf("%d", &iSelection); if (pAppend==NULL) printf("File not opened\n"); if(iSelection==1){ if (pAppend==NULL){ printf("File not opened\n"); return 0; } printf("Enter first name, last name id and GPA\n\n"); printf("Enter data seperated by spaces: "); scanf("%s%s%s%f", fName, lName, id, &gpa); fprintf(pAppend, "%s\t%s\t%s\t%.2f\n", fName, lName, id, gpa); fclose(pAppend); } if(iSelection==2) { if (pRead==NULL){ printf("File not opened\n"); return 0; } printf("Current student information\n\n"); printf("First Name\tLast Name\tID\tGPA\n"); fscanf(pRead, "%s%s%s%f", fName, lName, id, &gpa); while(!feof(pRead) ) { printf("%s\t\t%s\t\t%s\t%.2f\n", fName, lName, id, gpa); fscanf(pRead, "%s%s%s%f", fName, lName, id, &gpa); } fclose(pRead); } } }



LinkBack URL
About LinkBacks


