You are here:

C++/Bank Account program in C

Advertisement


Question
Apologies for not being clear in my previous e-mail.

Below is an answer to a question you sent on 12th August to another request. I would like to see the finished program in C language, if possible.
Regards and best wishes,

John English,





. define a struct bank_account to hold the information about an account in one place.

2. create an array of bank_account of size 10

3. define an int to keep track of the number of accounts.

4. in a loop,
    display a menu and accept user choice.
    if 'Q' quit, we're done
    if 'C' create account:
        if number of accounts is 10,
         print an error message
         back to beginning of loop
        else
         accept account information from the user
         set the values in the appropriate account in the array.
         increment number of accounts  
         back to beginning of loop
    if 'D' deposit
         get account number from user
         locate account with this number in array.
         if not found, print an error message
         else, get positive amount, process request, print receipt
         back to beginning of loop
    ...
    and so on for other requests


Answer
ok. here is my way of writing this program. (there are several other ways to do it).
*** warning: the program has not been tested.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

enum { MAX_ACCOUNTS = 10, NAMESZ = 128, ADDRSZ = 256 } ;
enum { CREATE = 'C', DEPOSIT = 'D', BALANCE = 'B', QUIT = 'Q' } ;

typedef struct account account ;
struct account
{
   int number ;
   char name[NAMESZ] ;
   char address[ADDRSZ] ;
   double balance ;
};

int get_account_number()
{
 int acnum = 0 ;
 char buffer[8] ;

 do
 {
   fputs( "enter account number: ", stdout ) ;
   fgets( buffer, sizeof(buffer), stdin ) ;
   acnum = atoi( buffer ) ;
   if( acnum < 1 ) fputs( "invalid account number\nre", stdout ) ;
 } while( acnum < 1 ) ;

 return acnum ;
}

/* returns the position of the account with account number == acct_number
  returns -1 if there is no such account */
int find_account( int acct_number, account* accounts, int num_accounts )
{
 int pos = 0 ;
 for( ; pos < num_accounts ; ++pos )
     if( accounts[pos].number == acct_number ) return pos ;
 return -1 ;
}

int create_account( account* accounts, int num_accounts )
{
 if( num_accounts == MAX_ACCOUNTS )
 {
   puts( "cannot create any more accounts" ) ;
   return 0 ;
 }
 else
 {
   account* ac = accounts + num_accounts ;
   int length ;
   char buffer[32] ;

   int duplicate = 0 ;
   do
   {
       ac->number = get_account_number() ;
       duplicate = find_account( ac->number, accounts, num_accounts ) != -1 ;
       if(duplicate)
         fputs( "an account with that number already exists\nre", stdout ) ;
   }while(duplicate) ;

   fputs( "enter account name: ", stdout ) ;
   fgets( ac->name, NAMESZ, stdin ) ;
   length = strlen( ac->name ) ;
   if( ac->name[ length - 1 ] == '\n' )
        ac->name[ length - 1 ] = 0 ;

   fputs( "enter address: ", stdout ) ;
   fgets( ac->address, ADDRSZ, stdin ) ;
   length = strlen( ac->address ) ;
   if( ac->address[ length - 1 ] == '\n' )
         ac->address[ length - 1 ] = 0 ;

   fputs( "enter balance: ", stdout ) ;
   fgets( buffer, sizeof(buffer), stdin ) ;
   ac->balance = atof( buffer ) ;

   return 1 ;
 }
}

void make_deposit( int acct_number, account* accounts, int num_accounts )
{
 int pos = find_account( acct_number, accounts, num_accounts ) ;
 if( pos == -1 ) puts( "there is no such account" ) ;
 else
 {
     char buffer[32] ;
     fputs( "enter amount: ", stdout ) ;
     fgets( buffer, sizeof(buffer), stdin ) ;
     accounts[ pos ].balance += atof( buffer ) ;
     printf( "account number: %d  balance: %f\n",
         acct_number, accounts[ pos ].balance ) ;
 }
}

void check_balance( int acct_number, account* accounts, int num_accounts )
{
 int pos = find_account( acct_number, accounts, num_accounts ) ;
 if( pos == -1 )
     puts( "there is no such account" ) ;
 else
     printf( "account number: %d\nname: %s\naddress: %s\nbalance: %f\n",
         acct_number, accounts[ pos ].name,
         accounts[ pos ].address, accounts[ pos ].balance ) ;
}

void display_menu()
{
 puts( "options\n-------\n  C: create account\n  D: deposit\n"
       "  B: check balance\n  Q: quit" ) ;
}

int get_choice()
{
 int choice = 0 ;
 const char* const valid_choices = "CDBQ" ;

 do
 {
   fputs( "enter choice: ", stdout ) ;
   fflush( stdin ) ;
   choice = toupper( fgetc( stdin ) ) ;
   fgetc( stdin ) ; /* discard newline */
   if( strchr( valid_choices, choice ) == NULL )
   {
     fputs( "invalid option\nre", stdout ) ;
     choice = 0 ;
   }
 } while( choice == 0 ) ;

 return choice ;
}

int main()
{
   account accounts[ MAX_ACCOUNTS ] ;
   int num_accounts = 0 ;
   int quit = 0 ;

   while( !quit )
   {
      int choice ;
      display_menu() ;
      choice = get_choice() ;
      switch( choice )
      {
         case CREATE:
         if( create_account( accounts, num_accounts ) )
         ++num_accounts ;
         break ;
         case DEPOSIT:
         make_deposit( get_account_number(), accounts, num_accounts ) ;
         break ;
         case BALANCE:
         check_balance( get_account_number(), accounts, num_accounts ) ;
         break ;
         case QUIT:
         quit = 1 ;
         break ;
      }
   }

   return 0 ;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.