Thread: Program is Closing

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    34

    Program is Closing

    Hello guys I 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
    Code:
    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:
    Program.exe has stopped working


    Here's my code
    Code:
    #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();
    }
    names.txt
    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
    Thank you in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I need help on this program because everytime the number of students reaches 7+,

    Have you really understood what the 4 means here?
    > students stud[4];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    34
    I dont :<

  4. #4
    Registered User
    Join Date
    Nov 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    34
    Its working now Thank you ! I changed it to stud[100]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program not closing
    By ewoods in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 07:25 AM
  2. Closing a program - what happens
    By C+/- in forum C++ Programming
    Replies: 9
    Last Post: 01-12-2007, 12:21 PM
  3. Closing program
    By DeepFyre in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2004, 06:22 PM
  4. Closing a program -help
    By Unregistered in forum C Programming
    Replies: 20
    Last Post: 04-22-2002, 11:06 AM
  5. How to restart a program without closing it...
    By D4050 in forum C++ Programming
    Replies: 16
    Last Post: 10-31-2001, 12:38 PM