Like Tree3Likes
  • 1 Post By anduril462
  • 1 Post By stahta01
  • 1 Post By CommonTater

crash error

This is a discussion on crash error within the C Programming forums, part of the General Programming Boards category; im doing my project on building a bank database account.. it compiles grand and you can put in multiple accounts ...

  1. #1
    Banned
    Join Date
    Apr 2011
    Posts
    19

    crash error

    im doing my project on building a bank database account.. it compiles grand and you can put in multiple accounts and delete them etc... but when i go to edit the details the program crashes can anyone help?

    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<string.h>
    
    
    #define SIZE 100
    
    /*definitions for the structures*/
     struct date
     {
         int day;
         int month;
         int year;
     };
     typedef struct date DATE;
         
     struct address
     {
         char town[50];
         char street[50];
         char county[50];
     };
     typedef struct address ADDRESS;
         
     struct details
     {
         int acc_number;
         char first_name[15];
         char surname [20];
         int age;
         ADDRESS address;
         DATE DOB;
         float current_balance;
         int overdraft;
         float lodge;
         float withdraw;
     };
     typedef struct details DETAILS;
         
     
     /*prototypes for fucntions*/
     void add_an_account(DETAILS []);
     void remove_an_account(DETAILS []);
     void edit_account(DETAILS []);
     void display_an_account(DETAILS []);
     void display_account_details(DETAILS []);
     void overdraft(DETAILS []);
     void lodge_money(DETAILS []);
     void withdraw(DETAILS []);
     void init_database(DETAILS []);
     int search_database(DETAILS [], int);
     int menu(void);
    
     
     
     
     void main()
     {
         DETAILS persons[SIZE];
         int menu_choice;
         
         init_database(persons);
         
         do
         {
             menu_choice = menu();
             
             switch(menu_choice)
             {
                 case 1 : add_an_account(persons);
                     break;
                 case 2 : remove_an_account(persons);
                     break;
                 case 3 : edit_account(persons);
                     break;
                 case 4 : display_an_account(persons);
                     break;
             }
        }
             while (menu_choice !=0);
    }
    
    /*add an employee function*/
    
    void add_an_account(DETAILS bank_account[])
    {
        int i=0; int count=0; int j;
        
        while(bank_account[i].acc_number!=0 && i < SIZE)
            i++;
        
        if ( i == SIZE)
            printf("/n The database is full, please delete an account to add a new one /n");
        else
        {
            printf("\n\n Enter your desired account number between 1 and 100 : ");
            do{
                scanf("%d", &bank_account[i].acc_number);
                fflush(stdin);
            }
            while(bank_account[i].acc_number <=0 );
                
            for(j=0;j<SIZE;j++)
            {
                if(bank_account[j].acc_number == bank_account[i].acc_number)
                    count++;
            }
            
            if(count == 1)
            {
                
            /* name of the new account */
            printf("\n please enter your first name : ");
            scanf("%15s", &bank_account[i].first_name);
            fflush(stdin);
            printf("\n please enter your surname : ");
            scanf("%20s", &bank_account[i].surname);
            fflush(stdin);
            
            /*date of birth for the new account*/
            printf("\n please enter your birth date\n");
            printf("\n     Day: (Max 2 digits) : ");
            scanf("%2d", &bank_account[i].DOB.day);
            fflush(stdin);
            printf("\n     Month: (Max 2 digits) : ");
            scanf("%2d", &bank_account[i].DOB.month);
            fflush(stdin);
            printf("\n     Year: (Max 2 digits) : ");
            scanf("%2d", &bank_account[i].DOB.year);
            fflush(stdin);
            printf("\n Enter your age : ");
            scanf("%2d", &bank_account[i].age);
            fflush(stdin);
            
            /*the address of the new account*/
            printf("\n please enter your address\n");
            printf("\n     Town: ");
            scanf("%s", &bank_account[i].address.town);
            fflush(stdin);
            printf("\n     Street: ");
            scanf("%s", &bank_account[i].address.street);
            fflush(stdin);
            printf("\n     county: ");
            scanf("%s", &bank_account[i].address.county);
            fflush(stdin);
            
            /* the balance of the new account
            printf("\n please enter your starting balance \n");
            scanf("%d", &bank_account[i].current_balance);
            fflush(stdin);*/
        }
        else
            printf("sorry this account number is already being used");
        }
    }
    
    /*delete an account*/
    /* deleting an account is done by replacing the account with a 0 in the array*/
    void remove_an_account(DETAILS bank_account[])
    {
        int account_number;
        int pos;
        
        printf("\nEnter account that you want to be removed(1-100): ");
        do 
            scanf("%3d", &account_number);
        while ( account_number <= 0 );
            
            pos = search_database(bank_account, account_number); 
        
        if( pos == SIZE)
            printf("\nThis account is not in the database\n");
        else
        {
            printf("\naccount %3d deleted", account_number);
            bank_account[pos].acc_number = 0;
            bank_account[pos].current_balance = 0;
            bank_account[pos].overdraft = 0;
            bank_account[pos].lodge = 0;
            bank_account[pos].withdraw = 0;
            bank_account[pos].DOB.day = 0;
            bank_account[pos].DOB.month = 0;
            bank_account[pos].DOB.year = 0;
            bank_account[pos].age = 0;
        }
    }
    
    /*display a bank account*/
    
    void display_an_account( DETAILS bank_account[] )
    {
        int account_number;
        int pos;
        
        printf("\nEnter account number desired(1 - 100): ");
        do
            scanf("%d", &account_number);
        while(account_number <=0);
            pos = search_database( bank_account, account_number);
        
        if(pos == SIZE)
            printf("This account is not in the database\n");
        else
            display_account_details( &bank_account[pos]);
    }
    
    /*edit an accounts details*/
    /*to edit the account you have to find the position of the account in the memory and change it*/
    
    void edit_account(DETAILS bank_account[])
    {
        int account_number;
        int j;
        
        printf("\nEnter the account number you wish to edit: ");
        do
            scanf("%3d", &account_number);
            while(account_number <=0);
            
            j = search_database(bank_account, account_number);
        
        if(j == SIZE)
            printf("\nThere is no account with this number\n");
        else
        {
            
            /*Replace the account holders first name*/
            
            printf("\nPlease enter the new first name desired: ");
            scanf("%s", bank_account[j].first_name);
            fflush(stdin);
            printf("\nPlease enter the new surname desired: ");
            scanf("%s", bank_account[j].surname);
            fflush(stdin);
        
        
            /*change the date of birth in the bank account*/
        
            printf("\nPlease enter the new date of birth desired\n");
            printf("Day:");
            scanf("%s", bank_account[j].DOB.day);
            fflush(stdin);
            printf("Month:");
            scanf("%s", bank_account[j].DOB.month);
            fflush(stdin);
            printf("Year:");
            scanf("%s", bank_account[j].DOB.year);
            fflush(stdin);
            printf("New age: ");
            scanf("%d", bank_account[j].age);
            fflush(stdin);
        
        
            /* change the address in the bank_account*/
        
            printf("\nPlease enter the new town desired: \n");
            scanf("%s", bank_account[j].address.town);
            fflush(stdin);
            printf("\nPlease enter the new street desired: \n");
            scanf("%s", bank_account[j].address.street);
            fflush(stdin);
            printf("\nPlease enter the new county desired: \n");
            scanf("%s", bank_account[j].address.county);
            fflush(stdin);
        }
    }
    
    
    /* search for an account*/
    
    int search_database(DETAILS bank_account[], int emp_number)
    {
        
        int i = 0;
        
        while(i<SIZE && bank_account[i].acc_number != emp_number)
            i++;
        return(i); 
    }
    
    
    
    /*display the details of an account*/
    
    void display_account_details(DETAILS *ptr)
    {
        printf("\n\n\n");
        printf("Account number: %d\n", ptr->acc_number);
        printf("First name: %s\n", ptr->first_name);
        printf("Surname: %s\n", ptr->surname);
        printf("Age: %d\n", ptr->age);
        printf("Date of birth: %d/%d/%d\n", ptr->DOB.day, ptr->DOB.month, ptr->DOB.year);
        printf("Town: %s\n street: %s\n County: %s\n",ptr->address.town,ptr->address.street,ptr->address.county);
        printf("Balance: %d", ptr->current_balance);
    }
    
    /*initialise database*/
    
    void init_database(DETAILS bank_account[])
    {
        int i;
        for(i=0;i<SIZE;i++)
        {
            bank_account[i].acc_number = 0;
            bank_account[i].current_balance = 0;
            bank_account[i].overdraft = 0;
            bank_account[i].lodge = 0;
            bank_account[i].withdraw = 0;
            bank_account[i].DOB.day = 0;
            bank_account[i].DOB.month = 0;
            bank_account[i].DOB.year = 0;
            bank_account[i].age = 0;
        }
    }
    
    /*menu function*/
    
    int menu(void)
    {
        int choice;
        
        /*display the menu to the user*/
        printf("\n\n 1: Add an account\n");
        printf("\n\n 2: delete an account\n");
        printf("\n\n 3: Edit an account\n");
        printf("\n\n 4: display an account\n");
        printf("\n\n 5: Exit the program\n");
        
        do
        {
            scanf("%d", &choice);
            fflush(stdin);
        }
        while (choice <0|| choice >4);
        
        return(choice);
        
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Your code does not "compile grand". You need to crank up the warning settings to the maximum on your compiler, and resolve the issues. Here's what my compiler reported:
    Code:
    $ gcc -Wall bank.c
    bank.c:57: warning: return type of ‘main’ is not ‘int’
    bank.c: In function ‘add_an_account’:
    bank.c:113: warning: format ‘%15s’ expects type ‘char *’, but argument 2 has type ‘char (*)[15]’
    bank.c:116: warning: format ‘%20s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’
    bank.c:137: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[50]’
    bank.c:140: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[50]’
    bank.c:143: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[50]’
    bank.c: In function ‘edit_account’:
    bank.c:240: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
    bank.c:243: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
    bank.c:246: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
    bank.c:249: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    bank.c: In function ‘display_account_details’:
    bank.c:293: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
    The first error is because void main is wrong. The correct form is int main(void), and return an int at the end, usually 0 for success. Also, fflush(stdin) is undefined, so it could theoretically cause your program to crash too. Here's an alternative: Cprogramming.com FAQ > Flush the input buffer.

    The rest are from incorrect usage of scanf and printf. The ones about 'char (*)[50] are because you have an ampersand you don't need. The name of an array serves as an address to the first element, which is what scanf needs for a %s, so lines like the following need the ampersand dropped:
    Code:
    scanf("%15s", &bank_account[i].first_name);
    The others are just plain wrong types. You can't run around willy-nilly mixing whatever variable types and format specifiers suit your fancy. Read up on scanf and printf documentation, particularly the parts about format specifiers. Note that, for simple types like integers and doubles, scanf does require the &.
    Salem likes this.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You use %s for an int in your edit function, if you don't get warnings about this, you should turn them up. It doesn't matter if your can compile if you ignore warnings that will lead to run time errors.

  4. #4
    Banned
    Join Date
    Apr 2011
    Posts
    19
    on my compiler i have no errors or warnings

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,553
    Quote Originally Posted by morrissey999 View Post
    on my compiler i have no errors or warnings
    I suggest getting a better compiler or learning to enable the warnings.

    Tim S.
    Salem likes this.

  6. #6
    Banned
    Join Date
    Apr 2011
    Posts
    19
    thanks anyway for the help i just used an abersand on everything and the program works grand thanks

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    A small lesson in C programming...
    Code:
    Building main.obj.
    E:\c_Code\Experiments\bank\main.c(58): warning #2181: Incorrect signature for entry-point 'main'; expected 'int __cdecl function(void)' but found 'void __cdecl function(void)'.
    E:\c_Code\Experiments\bank\main.c(114): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'char *' but found 'char (*)[15]'.
    E:\c_Code\Experiments\bank\main.c(117): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'char *' but found 'char (*)[20]'.
    E:\c_Code\Experiments\bank\main.c(138): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'char *' but found 'char (*)[50]'.
    E:\c_Code\Experiments\bank\main.c(141): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'char *' but found 'char (*)[50]'.
    E:\c_Code\Experiments\bank\main.c(144): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'char *' but found 'char (*)[50]'.
    E:\c_Code\Experiments\bank\main.c(241): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'char *' but found 'int'.
    E:\c_Code\Experiments\bank\main.c(244): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'char *' but found 'int'.
    E:\c_Code\Experiments\bank\main.c(247): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'char *' but found 'int'.
    E:\c_Code\Experiments\bank\main.c(250): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'int *' but found 'int'.
    E:\c_Code\Experiments\bank\main.c(294): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'double'.
    Building bank.exe.
    Done.
    ... "Compiles" does not mean "works"! Note that your code compiled in Pelles C, producing an EXE file... but it's goint to be printing a lot of garbage answers and may cause acccess violation errors at runtime.

    One of the biggest improvements in compilers over the last 5 or 6 years is their ability to report errors and warnings. Older compilers such as Turbo C will remain nicely quiet letting you compile and assume success. Newer compilers will try to warn you about impending problems at runtime.
    Last edited by CommonTater; 04-30-2011 at 02:01 PM.
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does it crash?
    By Guti14 in forum C++ Programming
    Replies: 14
    Last Post: 06-11-2010, 05:42 PM
  2. why does this crash?
    By MarlonDean in forum C++ Programming
    Replies: 55
    Last Post: 05-23-2008, 03:01 AM
  3. Crash SQL Course
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 01-04-2005, 08:37 AM
  4. Why does this crash??
    By jusluk2 in forum C++ Programming
    Replies: 6
    Last Post: 05-09-2003, 11:45 AM
  5. Crash after crash!
    By Dual-Catfish in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2002, 08:32 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21