lundi 27 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>

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

struct student {
        char first_name[50], last_name[50];
        int PassedUnit;
        int Year;
        int Rank;
        long sID;
        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","wt+");

        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. Sort   Records");
     printf("n ttt 6. 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 Firt Name  : ");
                scanf(" %s", e.first_name);

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

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

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

                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");

            printf("t                       ======== View All Record ========                          n");
            printf("t----------------------------------------------------------------------------------n");
            printf("tName            Family          Number     Unit        Average    Rank      Year  n");
            printf("t----------------------------------------------------------------------------------n");

           rewind(fp);
           while (fread(&e,recsize,1,fp) == 1){
                printf("t");
                printf("%-15s %-15s", e.first_name, e.last_name);
                //printf("     ");
                printf("%-12d", e.sID);
                //printf("   ");
                printf("%-12d", e.PassedUnit);
                //printf("   ");
                printf("%.2f", e.Average);
                //printf("   ");
                printf("%10d", e.Rank);
                //printf("   ");
                printf("%10d", e.Year);
                printf("n");
           }
           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");
                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 PassedUnit : ");
                scanf("%d", &e.PassedUnit);

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

                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", "wt");

              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':
            system("cls");



            system("cls");

            case '6':
              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