Thread: Bank record getting added on top of existing ones

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

    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
    948
    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
    39,661
    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,787
    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; 01-14-2020 at 11:58 PM.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    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

  6. #6
    Registered User
    Join Date
    May 2019
    Posts
    47
    ok i got this fixed ..thanks everyone for their input

    @salem what do you mean by that? i never said i am writting the code myself

    @hodor the first time i posted that project code ..at that time ..i was still learning..
    and can you please point me to any bankmanagement system code that meets modern standards? if you think my code is that nasty?

    critisizing my code or posting links of code that i am using is not helping me at all as i am newbie
    ..instead please post any updated code that you found useful and please dont tell me to just google as i already have done that..thanks
    Last edited by sash_007; 01-27-2020 at 08:29 PM.

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by sash_007 View Post
    ok i got this fixed ..thanks everyone for their input

    @salem what do you mean by that? i never said i am writting the code myself

    @hodor the first time i posted that project code ..at that time ..i was still learning..
    and can you please point me to any bankmanagement system code that meets modern standards? if you think my code is that nasty?

    critisizing my code or posting links of code that i am using is not helping me at all as i am newbie
    ..instead please post any updated code that you found useful and please dont tell me to just google as i already have done that..thanks
    I wasn't criticizing you I was criticizing the website. By "the code above" I was referring to the code in the links in my previous message, not your code; sorry that I didn't write that more clearly). I don't know if you're looking for a book or specifically a bank management system (edit: I tried to find an example of bank management system but failed. The difficulty is that bank management systems aren't normally written in C). Here's a free book that seems to be good: The C Book - Table of Contents
    Last edited by Hodor; 01-27-2020 at 09:04 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > and can you please point me to any bankmanagement system code that meets modern standards?
    Why?

    Seriously, the point of these exercises are for you to write your own code. If doesn't matter whether the one you write is 'rubbish' compared to anyone else's version, it's the fact that you wrote it yourself and made it do the things you want that makes it awesome.

    You will be never (let me repeat that - NEVER) better than the people you're copying from.

    Because sooner or later, someone will ask you to write code that has never been written before.
    If your only coding skill at that point is google + tinkering, you're screwed.

    But if you did your time and graft by writing code from scratch every time, you'll be golden.

    Choose your path.
    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.

  9. #9
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    you're copying
    google + tinkering
    This is why so many people think they can program.
    You can't.
    "without goto we would be wtf'd"

  10. #10
    Registered User
    Join Date
    May 2019
    Posts
    47
    I agree to your point and at the same time i disagree too(as this applies to programmers who got the hang of things not for newbies)

    when i am copying someones code that doesnt mean i will copy code forever
    if you see 10 codes you will eventually realise how things work

    if you dont know where to use for ,while or switch
    you will end up spending time in pointless directions wasting time

    and one thing i realised is..this forum never gives you solution to your problems unlike other popular forums like stackoverflow but most members give you big big dialogue what you could have done and your a big zero and bla bla bla.. no wonder this forum is less popular

    you cant judge anybody like that.. i am sorry i ended up in wrong forum
    Last edited by sash_007; 02-03-2020 at 12:05 AM.

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