Hello guysI need help on this program because everytime the number of students reaches 7+, the program is having a problem and starts closing..
Output of Program
Program.exe has stopped workingCode:Name: First Uno Age: 1 Sex: Male Name: Second Dos Age: 2 Sex: Male Name: Third Tres Age: 3 Sex: Male Name: Fourth Quatro Age: 4 Sex: Male Name: Fifth Sinco Age: 5 Sex: Male Name: Sixth Sais Age: 6 Sex: Male Name: Seventh Siete Age: 7 Sex: Male Number of Students: 7 Enter Your First Name:
Here's my code
names.txtCode:#include <stdio.h> #include <conio.h> #include <string.h> #include <windows.h> #include <windowsx.h> typedef struct { //declare a structure char fname[30]; //for each student record char lname[30]; int age; char sex[30]; }students; int main(void) { FILE *fpIn; int i,studnumber,b; char buffer[80]; students stud[4]; //make an array of student records fpIn=fopen("names.txt","r+"); if(!fpIn) { printf("Error opening file!\n"); return 1; } i=0; while(fgets(buffer, sizeof(buffer), fpIn)) { sscanf(buffer, "%s %[^,],%d,%s",stud[i].fname,stud[i].lname,&stud[i].age,&stud[i].sex); printf("Name: %s %s \nAge: %d \nSex: %s\n\n",stud[i].fname,stud[i].lname,stud[i].age,stud[i].sex); ++i; } studnumber=i; fclose(fpIn); printf("\nNumber of Students: %d \n",studnumber); fpIn=fopen("names.txt","a"); if(!fpIn){ printf("Error on opening file!\n"); return 1; } printf("\nEnter Your First Name: "); gets(stud[i].fname); printf("Enter Your Last Name: "); gets(stud[i].lname); printf("Enter Your Gender: "); gets(stud[i].sex); printf("Enter Your Age: "); scanf("%d",&stud[i].age); printf("Name: %s %s\nAge: %d\nGender: %s\n",stud[i].fname,stud[i].lname,stud[i].age,stud[i].sex); fprintf(fpIn,"%s %s,%d,%s\n",stud[i].fname,stud[i].lname,stud[i].age,stud[i].sex); fclose(fpIn); getch(); }
Thank you in advance!Code:First Uno,1,Male Second Dos,2,Male Third Tres,3,Male Fourth Quatro,4,Male Fifth Sinco,5,Male Sixth Sais,6,Male Seventh Siete,7,Male



LinkBack URL
About LinkBacks
I need help on this program because everytime the number of students reaches 7+, the program is having a problem and starts closing..


