|
-
February 13th, 2011, 02:38 PM
#1
Bank ATM Assignment
Hey everyone I'm a little confused with my C++ ATM Machine program. My program, at the moment needs the user to enter 10 accounts before he can operate it, however i've tried pre setting the code for 10 details in an array but i cannot do it. Hopefully Someone can help me.
Code:
#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;
typedef struct account_details
{
char firstname[10];
char lastname[10];
int accountnumber;
float balance;
int pnumber;
char accountstatus[9];
} account_details;
void dataloading(account_details accountdet[]);
int askselectioninput();
void deposit_amount(account_details accountdet[]);
void withdraw_amount(account_details accountdet[]);
void cancel_acc(account_details accountdet[]);
void display_acc(account_details accountdet[]);
void logout(account_details accountdet[]);
int main(){
cout<<"========================================="<<endl;
cout<<"========================================="<<endl;
cout<<"|| WELCOME TO THE ENCLAVE G-E-C-K ||"<<endl;
cout<<"========================================="<<endl;
cout<<"========================================="<<endl;
account_details accounts [10]; //the struct of account_details for the array accounts
int option =0; //for the option selection
//accounts[0].accountstatus * == {'Act'};
accounts[0].balance = 1000; //makes the account 0's balance = 1000
dataloading(accounts);
while (true){
option = askselectioninput();
if (option ==1)
{
deposit_amount(accounts);
}
else if (option ==2)
{
withdraw_amount(accounts);
}
else if (option ==3)
{
display_acc(accounts);
}
else if (option ==4)
{
cancel_acc(accounts);
}
else if (option ==5){
logout (accounts);
}
}
}
int askselectioninput(){
int option= 0;
cout<<"==============YOUR TRANSACTION CHOICES==================="<<endl;
cout<<"Press 1 for Deposit Press 2 for Withdraw"<<endl;
cout<<"press 3 for Display Balance Press 4 for Close the account"<<endl;
cout<<"===============Press 5 for Quit=========================="<<endl;
cin>>option;
if (option >0 && option <6){
return option;
}
else {
cout<<"--Incorrect Value--"<<endl<<endl;
return askselectioninput();
}
}
void dataloading(account_details accountdet[]){
int number;
int i;
for (i=0;i<2;i++)
{
cout<<"Enter your First Name: "<<endl;
cin>>accountdet [i].firstname;
cout<<"Enter your Last Name:"<<endl;
cin>>accountdet [i].lastname;
cout<<"Please enter your Pin: "<<endl;
cin>>accountdet [i].pnumber;
cout<<"Now please enter your Account Number: (0-9) "<<endl;
cin>>accountdet [i].accountnumber;
}
cout<<"Please Enter your account number for verification: "<<endl;
cin>>number;
cout<<"||==============x=============||"<<endl;
cout<<"|| LOGGED INTO GECK: ||"<<endl;
cout<<"||===========================||"<<endl;
cout<<"-------------------------------------------"<<endl;
cout<<"Welcome MR/MRS/Miss/Master: "<<accountdet [number].lastname<<endl;
cout<<"Your current Balance is: "<<accountdet [number].balance<<endl;
cout<<"Your Account number is: "<<accountdet [number].accountnumber<<endl;
cout<<"-------------------------------------------"<<endl;
}
void display_acc(account_details accountdet[])
{
int number;
number = (accountdet[number].accountnumber);
cout<<"=========================================================="<<endl;
cout<<"------------------ USER DETAILS ------------------- "<<endl;
cout<<"Welcome: "<<accountdet [number].firstname<<accountdet [number].lastname<<endl<<endl;
cout<<"Your current Balance is: "<<accountdet [number].balance<<endl;
cout<<"Your Account number is: "<<accountdet [number].accountnumber<<endl;
cout<<"Your Account Status is: "<<accountdet [number].accountstatus<<endl;
if (accountdet[number].balance <0)
{
cout<<"====YOU HAVE BECOME OVERDRAWN!===="<<endl;
}
cout<<"============================================================"<<endl<<endl;
}
void withdraw_amount(account_details accountdet[])
{
int number;
number = (accountdet[number].accountnumber);
float amount;
cout<<"--------------------------------------------------"<<endl;
cout<<"-- Withdraw from G-E-C-K --"<<endl; //Function that executes if F is chosen
cout<<"--------------------------------------------------"<<endl;
cout<<"Please Enter your amount to withdraw: "<<endl;
cin>>amount;
accountdet[number].balance = accountdet[number].balance + amount;
}
void cancel_acc(account_details accountdet[])
{
int number;
number = (accountdet[number].accountnumber);
char firstname;
char lastname;
cout<<"=========================================================="<<endl;
cout<<" CLOSING THIS ACCOUNT: "<<endl;
//accountdet[0].firstname;
//lastname = accountdet[0].lastname;
//lastname = 'N/A';
//firstname = 'N/A';
cout<<accountdet[number].firstname;
cout<<accountdet[number].lastname;
cout<<"============================================================"<<endl<<endl;
}
void deposit_amount(account_details accountdet[])
{
int number;
number = (accountdet[number].accountnumber);
float amount;
cout<<"--------------------------------------------------"<<endl;
cout<<"-- Deposit into G-E-C-K --"<<endl; //Function that executes if F is chosen
cout<<"--------------------------------------------------"<<endl;
cout<<"Please Enter your amount to deposit: "<<endl;
cin>>amount;
accountdet[number].balance = accountdet[number].balance - amount;
}
void logout (account_details accountdet[])
{
cout<<"==========================="<<endl;
cout<<" YOU ARE NOW LOGGED OUT"<<endl;
cout<<"==========================="<<endl<<endl;
dataloading(accountdet);
}
-
February 13th, 2011, 03:04 PM
#2
Re: Bank ATM Assignment
Perhaps you could give more details on what the problem is.
Note, if anyone enters a name longer than 9 characters it could break your program, or worse, allow a hacker to take control of it. To solve this, prefer to use std::string rather than char arrays for string handling or otherwise limit the amount of input into a char array to 9 bytes.
-
February 13th, 2011, 03:19 PM
#3
Re: Bank ATM Assignment
Well I have to create a program for University that operates 10 pre created accounts. These accounts will be accessed by the user via entering their pin or account number which gives them access to the account. However, As my code shows, I've promted them to input the number 0-9 for the account and my arrays do not change.
-
February 13th, 2011, 03:59 PM
#4
Re: Bank ATM Assignment
 Originally Posted by TBsparky
Well I have to create a program for University that operates 10 pre created accounts.
You can't just dismiss what was told to you by Lindley.
Your program will overwrite memory if this happens:
Code:
Enter your First Name:
Dimitri
Enter your Last Name:
Popodopoulis
You make no protection to see if a name is longer than 9 characters (if you include the terminating NULL). You shouldn't just leave these things alone -- these types of bugs is what makes C++ programs unstable and crash.
You should fix this either by actually limiting the amount or using std::string and getline() to get the input.
These accounts will be accessed by the user via entering their pin or account number which gives them access to the account. However, As my code shows, I've promted them to input the number 0-9 for the account and my arrays do not change.
Have you used your debugger to single-step through your program to see what's wrong? Most compilers of today come with debuggers, and usage of the debugger is mandatory if you write a program and not know why it goes a certain path or a variable isn't what it's supposed to be.
Regards,
Paul McKenzie
-
February 13th, 2011, 04:08 PM
#5
Re: Bank ATM Assignment
 Originally Posted by Paul McKenzie
You can't just dismiss what was told to you by Lindley.
Your program will overwrite memory if this happens:
Code:
Enter your First Name:
Dimitri
Enter your Last Name:
Popodopoulis
You make no protection to see if a name is longer than 9 characters (if you include the terminating NULL). You shouldn't just leave these things alone -- these types of bugs is what makes C++ programs unstable and crash.
You should fix this either by actually limiting the amount or using std::string and getline() to get the input.
Have you used your debugger to single-step through your program to see what's wrong? Most compilers of today come with debuggers, and usage of the debugger is mandatory if you write a program and not know why it goes a certain path or a variable isn't what it's supposed to be.
Regards,
Paul McKenzie
I'm sorry, I honestly didn't see what Lidney had said I'll implement that after i have sorted this array business. I need to use an array for the accounts, its a requirement, from my code i cant really use the getline()
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|