mardi 21 juin 2016

Program stops after enter a number

I Write this program but when I enter '1' then enter First and Last name program Asked me for entering Student ID. After I enter Student ID it closed and returned -1073741819 (0xC0000005)

Please help me to Solve This Problem. I writed this in Code::Blocks.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main() {
FILE *fp, *ft;
char another, choice;

struct student {
    char first_name[50], last_name[50];
    int passed;
    int Year;
    int Rank;
    int stu_num;
    float Average;
};

struct student e;
char xfirst_name[50], xlast_name[50];
long int recsize;

fp=fopen("users.txt","rb+");

if (fp == NULL) {
    fp = fopen("users.txt","wb+");

    if (fp==NULL)
    {
         puts("Cannot open file");
         return 0;
    }
}


 recsize = sizeof(e);

 while(1) {
 system("cls");

 printf("tt====== STUDENT INFORMATION SYSTEM ======");
 printf("nn                                          ");
 printf("nn");
 printf("n ttt 1. Add    Records");
 printf("n ttt 2. List   Records");
 printf("n ttt 3. Modify Records");
 printf("n ttt 4. Delete Records");
 printf("n ttt 5. Exit   Program");
 printf("nn");
 printf("ttt Select Your Choice :=> ");
 fflush(stdin);
 choice = getche();
 switch(choice)
 {
  case '1' :
        fseek(fp,0,SEEK_END);
        another ='Y';
        while(another == 'Y' || another == 'y')
        {
            system("cls");
            printf("Enter the First Name  : ");
            scanf("%s", e.first_name);

            printf("Enter the Last Name  : ");
            scanf("%s", e.last_name);

            printf("Enter the sID        : ");
            scanf("%d", e.stu_num);

            printf("Enter the passed     : ");
            scanf("%d", e.passed);

            printf("Enter the Year       : ");
            scanf("%d", e.Year);

            printf("Enter the Rank       : ");
            scanf("%d", e.Rank);

            printf("Enter the Average    : ");
            scanf("%f", e.Average);

            fwrite(&e,recsize,1,fp);
            printf("n Add Another Record (Y/N) ");
            fflush(stdin);
            another = getchar();
        }
        break;
case '2':
        system("cls");
       rewind(fp);
       printf("=== View the Records ===");
       printf("n");
       while (fread(&e,recsize,1,fp) == 1){
       printf("n");
       printf("n", &e.first_name, "t", &e.last_name);
       printf("n");
       printf("npassed: ", e.passed);
       printf("nstu_num: ", e.stu_num);
       printf("nYear: ", e.Year);
       printf("nRank: ", e.Rank);
       printf("nAverage: ", e.Average);
       }
       printf("nn");
       system("pause");
       break;

case '3' :
        system("cls");
      another = 'Y';
      while (another == 'Y'|| another == 'y')
      {
          printf("n Enter the last name of the student : ");
          scanf("%s", &xlast_name);

        rewind(fp);
        while (fread(&e,recsize,1,fp) == 1)
        {
            if (strcmp(e.last_name,xlast_name) == 0)
            {
              system("cls");
            printf("Enter the Firt Name  : ");
            scanf("%s", e.first_name);

            printf("Enter the Last Name  : ");
            scanf("%s", e.last_name);

            printf("Enter the passed : ");
            scanf("%d", e.passed);

            printf("Enter the stu_num        : ");
            scanf("%d", e.stu_num);

            printf("Enter the Year       : ");
            scanf("%d", e.Year);

            printf("Enter the Rank       : ");
            scanf("%d", e.Rank);

            printf("Enter the Average    : ");
            scanf("%f", e.Average);

            fseek(fp, - recsize, SEEK_CUR);
            fwrite(&e,recsize,1,fp);
            break;
            }
            else
            printf("record not found");
        }
        printf("n Modify Another Record (Y/N) ");
            fflush(stdin);
            another = getchar();
        }
        break;


 case '4':
   system("cls");
       another = 'Y';
      while (another == 'Y'|| another == 'y')
      {
          printf("n Enter the last name of the student to delete : ");
          scanf("%s", &xlast_name);

          ft = fopen("temp.dat", "wb");

          rewind(fp);
          while (fread (&e, recsize,1,fp) == 1)

             if (strcmp(e.last_name,xlast_name) != 0)
            {
                fwrite(&e,recsize,1,ft);
            }
            fclose(fp);
            fclose(ft);
            remove("users.txt");
            rename("temp.dat","users.txt");

            fp=fopen("users.txt","rb+");

            printf("n Delete Another Record (Y/N) ");
            fflush(stdin);
            another = getchar();
          }

          break;

          case '5':
          fclose(fp);
          printf("nn");
          printf("tt     THANK YOU FOR USING THIS SOFTWARE");
          printf("nn");
          exit(0);
      }
 }


system("pause");
return 0;
}

Very Thanks for your help.

Aucun commentaire:

Enregistrer un commentaire