HI
i have a question i have a code for a simple bank account for my uni assignment below, and i need to know these things
1. when i exit the program all data is lost so i need to devise a means of storing the data so that i can start where i left off.
2. make two improvements to the code.
If you can help please explain in lehmans terms bit by bit as i have no idea how to do c,i find it very hard please help and if you do your reward would be knowing the fact that you helped me pass this years exams!!
thanks leigh, here it is:
A simple bank account
---------------------
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct acc_type
{
char name[50];
float balance;
};
struct acc_type account[20];
int num_acc;
int current;
int name_check(char *);
void new_account();
void manage_account();
void add_money();
void take_money();
void close_acc();
void get_name(char *);
/*Start main program*/
int main()
{
char menuitem;
num_acc=0;
while(1)
{
printf("\n\nSelect from the menu\n1: new account\n"
"2: manage an existing account\n"
"3: finish this program\n");
menuitem = getch();
switch(menuitem)
{
case '1': new_account();
break;
case '2': manage_account();
break;
case '3': return 0;
}
}
return 0;
}
/*Function new_account*/
void new_account()
{
char acc_name[50];
char temp;
float value;
printf("\nEnter the name on the account: ");
get_name(acc_name);
if(acc_name[0] == '\0') return;
if (name_check(acc_name))
{
printf("\nThat name already exists!\n");
return;
}
printf("\nEnter the starting balance: %c",156);
temp=getch(); //read return key from input buffer*/
scanf("%f",&value);
strcpy(account[num_acc].name,acc_name);
if(value < 0)
{
printf("No negative balances allowed\n"
"Balance will be set to zero\n");
value = 0;
}
account[num_acc].balance=value;
num_acc++;
return;
}
/*Function manage_account*/
void manage_account()
{
char acc_name[50],selection;
printf("\nEnter the account holder's name: ");
get_name(acc_name);
if(acc_name[0] == '\0') return;
if(!name_check(acc_name))
{
printf("\nThat name does not exist!\n");
return;
}
printf("\nSelect from the following menu\n"
"1: add money to account\n"
"2: withdraw money from account\n"
"3: close account\n");
selection=getch();
switch(selection)
{
case '1': add_money();
break;
case '2': take_money();
break;
case '3': close_acc();
}
}
/*Function name_check*/
int name_check(char *customer)
{
int count;
for (count=0 ; count < num_acc ; count++)
{
if (!strcmp(customer,account[count].name))
{
current = count;
return(1);
}
}
return(0);
}
/*Function add_money*/
void add_money()
{
float add_sum;
char dummy;
printf("\nThe current balance for %s is %c%4.2f",account[current].name,156,
account[current].balance);
printf("\nHow much do you want to add to the account? %c",156);
dummy=getch();//clear out input buffer
scanf("%f",&add_sum);
if(add_sum < 0)
{
printf("\nNo negative sums allowed here");
return;
}
account[current].balance += add_sum;
printf("\nThe new balance is %c%4.2f",156,account[current].balance);
return;
}
/*Function take_money*/
void take_money()
{
float take_sum;
char dummy;
printf("\nThe current balance for %s is %c%4.2f",account[current].name,156,
account[current].balance);
if(account[current].balance == 0)
{
printf("\nThis account has zero balance - no overdraft facility!");
return;
}
printf("\nHow much do you want to take from the account? %c",156);
dummy=getch();//clear out input buffer
scanf("%f",&take_sum);
if(take_sum < 0)
{
printf("\nNo negative sums allowed here");
return;
}
if(take_sum > account[current].balance)
{
printf("\nInsufficient funds!");
return;
}
account[current].balance -= take_sum;
printf("\nThe new balance is %c%4.2f",156,account[current].balance);
}
/*Function close_acc*/
void close_acc()
{
char prompt;
printf("\nConfirm closure of account name %s\ny/n ",
account[current].name);
prompt = getch();
if (prompt == 'y')
{
num_acc--;
strcpy(account[current].name,account[num_acc].name);
account[current].balance = account[num_acc].balance;
printf("\nAccount closed");
return;
}
if (prompt == 'n')
{
printf("\nAccount not closed");
}
return;
}
/*Function get name*/
void get_name(char *name)
{
int count;
char temp;
count=0;
while ((temp = getch()) != 13)
{
if(((temp > 64) && (temp < 91)) || ((temp > 96) && (temp < 123))||(temp == 32))
{
putchar(temp);
name[count]=temp;
count++;
}
if((temp == 8)&&(count > 0))
{
putchar(temp);
putchar(32);
putchar(temp);
count--;
}
}
name[count]='\0';
}