-
Just a HINT
I just want a hint. Im learning arrays in combination with functions i doing a bank program that can track of certain amount of account (100). Can someone get me started on the Array. I think i have the functions worked out pretty good but need a hint on how incorporate the array for the accounts
[code]
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int menu();
int accountnumber();
void balancefunction (float); //fuction for Balance
float depositfunction(float); //function for deposit
float withdrawalfunction(float);//function for withdrawal
int main() //main function
{
int accountnumber();
float balance=0;
int s;
int account;
const int customernumber[50];
account= accountnumber();
s=menu(); //calling function
while(s!=5) //condition leading into my switch
{
switch (s)
{
case 1:
balance=depositfunction (balance);
break;
case 2:
balance=withdrawalfunction(balance);
break;
case 3:
balancefunction(balance);
break;
case 4:
account=accountnumber();
break;
}
cout<<"Your new balance is: "<<balance<<endl;
s=menu(); //calling function again to repeat the menu
}
cout<<"Good Bye"<<endl;
return 0;
}
int menu() //function
{
int x; //declaring varable
cout<<endl;
cout<<"*****Welcome to the ATM Services*****"<<endl;
cout<<endl;
cout<<"Please select 1-4: "<<endl;
cout<<"______________"<<endl;
cout<<" Menu "<<endl;
cout<<"______________"<<endl;
cout<<endl;
cout<<"1. Deposit Money"<<endl;
cout<<"2. Withdrawal Money"<<endl;
cout<<"3. Balance"<<endl;
cout<<"4. New Account number"<<endl;
cout<<"5. Quit"<<endl;
cout<<endl;
cin>>x;
while ((x!=1)&&(x!=2)&&(x!=3)&&(x!=4)&&(x!=5))
{
cout<<"Error Selection must be 1-4"<<endl;
cout<<"Please Try again"<<endl;
cin>>x;
}
return x;
}
void balancefunction(float balance)
{
cout<<"The balance is: "<<balance<<endl;//balance
}
float depositfunction(float balance)
{
float d;
cout<<"How much would you like to deposit: "<<endl;
cin>>d;//user input
balance=balance+d;//formula
balancefunction(balance);//calling the balance function
return balance
}
float withdrawalfunction(float balance)//withdrawal fuction, returning the value balance
{
float w;
cout<<"How much would you like to ithdrawal: "<<endl;
cin>>w;
balancefunction(balance);
if (balance-w<0)
cout<<"Sorry, not enough Funds in account"<<endl;
balance=balance-25;
}
else//condition is met then, goes into the next step
{
balance=balance-w;//balance formula
}
return balance;
}
int accountnumber()
{
int a;
cout<<"Please Enter your account number: "<<endl;
cin>>a;
while (a<0||a>49)
{
cout<<"Wrong account number"<<endl;
cout<<"Please try again: "<<endl;
cin>>a;
}
return a;
}
[code/n]
-
Ideally, you want to use classes for this. It would be much cleaner. Then you just use an array of classes. However, if you have never used classes, you'd probably be best served starting with a simple structure. Here is a quick example:
Code:
struct Account
{
char name[80];
float balance;
};
Then you'd make an array of these accounts, and change their names and what not when you need to.
An array is best thought of as a list of items. Arrays start at zero and go to "size-1". Like so:
Code:
struct Account AllAccounts[10];
AllAccounts[0].balance = 5.00;
...
AllAccounts[9].balance = 1000.00;
See here, we start at zero and go through nine. It looks like you understand the basics of arrays, but structures may be new to you.
Ideally you'd use classes, but structs should suffice.
Quzah.