Thread: Bank record getting added on top of existing ones

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    45

    Exclamation Bank record getting added on top of existing ones

    Hello friends

    I was trying to make a bank record system following an existing code
    but when i add new account its getting added as i can see in record.dat
    but when i try to add another new account with same acc_no its not showing account already exists error instead of that overwritting the existing one

    here is the code
    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<windows.h>
     int i,j;
     int main_exit;
     //void menu();
     struct date{
         int month,day,year;
     };
     struct{
         char name[60];
         int acc_no,age;
         char address[60];
         char citizenship[15];
         double phone;
         char acc_type[10];
         float amt;
         struct date dob;
         struct date deposit;
         struct date withdraw;
    }add,upd,check,rem,transaction;
    
    
    //interest function
    float interest(float t,float amount,int rate)
    {
        float SI;
        SI = (rate*t*amount)/100;
        return (SI);
    }
    
    
    //delay function
    void fordelay(int j)
    {
        int i,k;
        for(i=0;i<j;i++)
        {
            k = i;
        }
    }
    
    
    //new account
    void new_acc()
    {
        int choice;
        FILE *ptr;
        ptr = fopen("record.dat","a+");
        account_no:
        system("cls");
        printf("\t\t\t\xB2\xB2\xB2\ ADD RECORD \xB2\xB2\xB2\xB2");
        printf("Enter Today's Date(mm/dd/yyyy: ");
        scanf("%d/%d/%d",&add.deposit.month,&add.deposit.day,&add.deposit.year);
        printf("\nEnter the account number:");
        scanf("%d",&check.acc_no);
        while(fscanf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citizenship,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
        {
            if(check.acc_no = add.acc_no)
            {
                printf("Account no. already in use!");
                fordelay(1000000000);
                goto account_no;
            }
    
    
        }
        add.acc_no = check.acc_no;
        printf("Enter the name: ");
        scanf("%s",add.name);
        printf("Enter the date of birth(mm/dd/yyyy): ");
        scanf("%d/%d/%d",&add.dob.month,&add.dob.year,&add.dob.year);
        printf("\nEnter the age: ");
        scanf("%d",&add.age);
        printf("\nEnter the address: ");
        scanf("%s",add.address);
        printf("\nEnter the citizenship number: ");
        scanf("%s",add.citizenship);
        printf("\nEnter the phone number: ");
        scanf("%lf",&add.phone);
        printf("\nEnter the amount to deposit: ");
        scanf("%f",&add.amt);
        printf("\nType of account:\n\t#Savings\n\t#Current\n\t#Fixed(for 1 year)\n\t#Fixed(for 2 years)\n\t#Fixed(for 3 years)\n\n\tEnter your choice:");
        scanf("%s",add.acc_type);
        fprintf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
        fclose(ptr);
        printf("\nAccount created successfully!");
        add_invalid:
        printf("\n\n\n\t\tEnter 1 to go to main menu and 0 to exit: ");
        scanf("%d",&main_exit);
        system("cls");
        if(main_exit==1)
        {
            menu();
        }
        else if(main_exit==0)
        {
            close();
        }
        else
        {
            printf("\nInvalid!\a");
            goto add_invalid;
        }
    }
    
    //edit account
    void edit()
    {
        int choice,test= 0;
        FILE *old,*newrec;
        old = fopen("record.dat","r");
        newrec = fopen("new.dat","w");
    
    
        printf("\nEnter the account no. of customer whose info you want to change:");
        scanf("%d",&upd.acc_no);
        while(fscanf(old,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citizenship,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
        {
          if(add.acc_no == upd.acc_no)
          {
              test = 1;
              printf("\nWhich information do you want to change?\n1.Address\n2.phone\n\nEnter your choice(1 for address and 2 for phone):");
              scanf("%d",&choice);
              system("cls");
              if(choice==1)
              {
                  printf("Enter the new address:");
                  scanf("%d",upd.address);
                  fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,upd.address,add.citizenship,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
                  system("cls");
                  printf("Changes saved!");
              }
              else if(choice ==2)
              {
                  printf("Enter the new phone number:");
                  scanf("%lf",&upd.phone);
                  fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,upd.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
                  system("cls");
                  printf("Changes saved!");
              }
    
    
          }
          else
          {
              fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
    
    
          }
    
    
        }
          fclose(old);
          fclose(newrec);
          remove("record.dat");
          rename("new.dat","record.dat");
          if(test!=1)
          {
              system("cls");
              printf("\nRecord not found!!\a\a\a");
              edit_invalid:
                  printf("\nEnter 0 to try again,1 to return to main menu and 2 to exit:");
                  scanf("%d",&main_exit);
                  system("cls");
                  if(main_exit==1)
                  {
                      menu();
                  }
                  else if(main_exit==2)
                  {
                      close();
                  }
                  else
                  {
                       printf("\nInvalid !\a");
                       goto edit_invalid;
                  }
          }
          else
          {
             printf("\n\n\nEnter 1 to go to main menu and 0 to exit:");
             scanf("%d",&main_exit);
             system("cls");
             if(main_exit==1)
             {
                 menu();
             }
             else
             {
                 close();
    
    
             }
          }
    }
    
    //Close function
    void close()
    {
        printf("\n\n\nThis C project is developed by Code with C Team!");
    }
    //menu function
    void menu()
    {
        int choice;
        system("cls");
        system("color 9");
        printf("\n\n\t\t\tCUSTOMER ACCOUNT BANKING MANAGEMENT SYSTEM");
        printf("\n\n\n\t\t\t\xB2\xB2\xB2\xB2\xB2\xB2\xB2 WELCOME TO MAIN MENU \xB2\xB2\xB2\xB2\xB2\xB2\xB2");
        printf("\n\n\t\t1.Create new account\n\t\t2.Update account\n\t\t3.For transactions\n\t\t4.Checking details of existing account\n\t\t5.Closing existing account\n\t\t6.View Customer's list\n\t\t7.Exit\n\n\n\n\n\t\tEnter your choice:");
        scanf("%d",&choice);
    
    
        system("cls");
        switch(choice)
        {
        case 1:
            new_acc();
            break;
        case 2:
            edit();
            break;
       
        }
    
    
    
    
    
    
    }
    
    int main()
    {
        //new_acc();
        menu();
        return 0;
    }
    there is also another issue i noticed when i try to edit existing account the whole record is getting wiped out

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    678
    Does your compiler have warnings enabled? You should have gotten a warning from this line:

    Code:
            if(check.acc_no = add.acc_no)
    Frankly, there are a lot of problems with this code. For one, menu() is mutually recursive with new_acc() and edit(), second, there's superfluous use of goto, and third, the fordelay() function doesn't do anything when it's compiled with basically any compiler from the last 20 years (unless optimizations are turned off). I suspect this code is originally from either 1989 (give or take a few years) or somewhere that still uses Turbo-C.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,605
    bank management system.c
    Get your money back, and stop trying to copy/paste your way to knowledge.
    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.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,674
    Edit:
    Huh, Storing structure data in txt file instead of dat file?

    Been working on this same problem for 7 months?! Maybe it's not for university after all

    Mini Project in C Bank Management System | Code with C (same code as on the Course "Hero" site)
    Last edited by Hodor; 1 Week Ago at 11:58 PM.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,674
    Quote Originally Posted by sash_007 View Post
    Hello friends

    I was trying to make a bank record system following an existing code
    but when i add new account its getting added as i can see in record.dat
    but when i try to add another new account with same acc_no its not showing account already exists error instead of that overwritting the existing one

    there is also another issue i noticed when i try to edit existing account the whole record is getting wiped out
    If you're doing this as a self learning exercise my suggestion would be to find a better website or book. The code above and all the code on the codewithc website that I looked at is horrific. I'm not saying this to be nasty but because that website is a waste of your time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. record isn't added to file
    By narniat in forum C Programming
    Replies: 4
    Last Post: 02-23-2019, 02:41 PM
  2. Replies: 9
    Last Post: 03-15-2016, 07:09 AM
  3. Update Record & Delete Record in File.
    By unsafe_pilot1 in forum C Programming
    Replies: 13
    Last Post: 05-18-2008, 07:22 AM
  4. About 5 spaces added out of no where!
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-16-2005, 10:53 PM

Tags for this Thread