hi i have written a program for A T M . i have finished the program and its working i wanted to add few more steps . when i enter the correct account number it goes to next step which is " Options " where one select the options and executes . but when the user enters wrong account number the program should end and it shouldnt ask for options but i am not able to do that, so i am stuck in that and in main function is there any way where i can skip writing all the given data again in switch condition and write it only once so that when user enters his account number the corresponding number should get select ....can you guys help me out .
this is my program
// WAP to enter name , account number of 5 customers and check whether the entered account no is valid or not and print out details of withdrawal or deposit and show the remaining balance ..
// Transfer the money from one account to another
#include <iostream>
using namespace std;
class customer
{
public:
string name;
int accountNumber;
int balance;
};
class atm
{
public:
customer obj[5];
int withDraw;
int Deposit;
int currentUserAccountID;
int validNumber(int accountNo);
void Options();
};
int atm::validNumber(int accountNo)
{
int matchedAccount;
for (int i=0;i<=4;i++)
{
if (obj[i].accountNumber==accountNo)
{
cout<<"\nThe account number is valid"<<endl;
matchedAccount=i;
return matchedAccount;
}
}
return 0;
}
void atm::Options()
{
int options;
cout<<"\nEnter options for cash withdrawal,deposit and transfer:"<<endl;
cout<<"\nPress 1 for withdrawal or Press 2 for deposit or Press 3 for transfer "<<endl;
cin>>options;
switch (options)
{
case 1:
cout<<"\nEnter amount of cash you want to withdraw :"<<endl;
/ WAP to enter name , account number of 5 customers and check whether the entered account no is valid or not and print out details of withdrawal or deposit and show the remaining balance ..
// Transfer the money from one account to another
#include <iostream>
usingnamespace std;
class customer
{
public:
string name;
int accountNumber;
int balance;
};
class atm
{
public:
customer obj[5];
int withDraw;
int Deposit;
int currentUserAccountID;
int validNumber(int accountNo);
void Options();
};
int atm::validNumber(int accountNo)
{
int matchedAccount;
for (int i=0;i<=4;i++)
{
if (obj[i].accountNumber==accountNo)
{
cout<<"\nThe account number is valid"<<endl;
matchedAccount=i;
return matchedAccount;
}
}
return 0;
}
void atm::Options()
{
int options;
cout<<"\nEnter options for cash withdrawal,deposit and transfer:"<<endl;
cout<<"\nPress 1 for withdrawal or Press 2 for deposit or Press 3 for transfer "<<endl;
cin>>options;
switch (options)
{
case 1:
cout<<"\nEnter amount of cash you want to withdraw :"<<endl;
cin>>withDraw;
withDraw=obj[currentUserAccountID].balance - withDraw;
cout<<"\nRemaining Ba lance after withdrawing :"<<withDraw<<endl;
break;
case 2:
cout<<"\nEnter amount of cash you want to deposit :"<<endl;
cin>>Deposit;
Deposit=Deposit+obj[currentUserAccountID].balance;
cout<<"\nBalance After Depositing :"<<Deposit<<endl;
break;
case 3 :
int accountNo2;
int secondUserAccountID;
int transferAmount;
cout<<"Enter account number 2 :"<<endl;
cin>>accountNo2;
secondUserAccountID=validNumber(accountNo2);
if (secondUserAccountID!=0)
{
cout<<"Enter the amount you want to transfer :"<<endl;
cin>>transferAmount;
obj[currentUserAccountID].balance = obj[currentUserAccountID].balance-transferAmount;
obj[secondUserAccountID].balance = obj[secondUserAccountID].balance+transferAmount;
cout<<"Your Balance After Transfer =<<obj[currentUserAccountID].balance<<endl;
}
break;
default :
cout<<"Wrong option try again "<<endl;
break;
}
}
int main (int argc, const char * argv[])
{
// var init
atm object;
int accNumber;
// input acc num
cout<<"Enter the account number :"<<endl;
cin>>accNumber;
// customer database
object.obj[0].name = "abcd " ;
object.obj[0].accountNumber =10021;
object.obj[0].balance=24000;
object.obj[1].name = "ewrw " ;
object.obj[1].accountNumber =10022;
object.obj[1].balance=14000;
object.obj[2].name = "ersfd " ;
object.obj[2].accountNumber =10024;
object.obj[2].balance=10000;
object.obj[3].name = "cvf ";
object.obj[3].accountNumber =10093;
object.obj[3].balance=34000;
object.obj[4].name = "ad " ;
object.obj[4].accountNumber =10031;
object.obj[4].balance=12000;
// check validity
object.currentUserAccountID = object.validNumber(accNumber);
switch (accNumber)
{
case 10021:
cout<<"\nCustomer name :"<<object.obj[0].name<<endl;
cout<<"Account number :"<<object.obj[0].accountNumber<<endl;
cout<<"Balance "<<object.obj[0].balance<<endl;
break;
case 10022:
cout<<"\nCustomer name :"<<object.obj[1].name<<endl;
cout<<"Account number :"<<object.obj[1].accountNumber<<endl;
cout<<"Balance "<<object.obj[1].balance<<endl;
break;
case 10024:
cout<<"\nCustomer name :"<<object.obj[2].name<<endl;
cout<<"Account number :"<<object.obj[2].accountNumber<<endl;
cout<<"Balance "<<object.obj[2].balance<<endl;
break;
case 10093:
cout<<"\nCustomer name :"<<object.obj[3].name<<endl;
cout<<"Account number :"<<object.obj[3].accountNumber<<endl;
cout<<"Balance "<<object.obj[3].balance<<endl;
break;
case 10031:
cout<<"\nCustomer name :"<<object.obj[4].name<<endl;
cout<<"Account number :"<<object.obj[4].accountNumber<<endl;
cout<<"Balance "<<object.obj[4].balance<<endl;
break;
default: cout<<" Enter Again "<<endl;
break;
}
object.Options();
}
Report
I had to modify the program to get it to compile. You need to add #include <string> at the top and main should return something, just add return 0; at the end of main. And there's a syntax error with one of the log lines.
Double spacing your code doesn't help.
You have a number of problems.
1. The classes don't have constructors and so the class members aren't initialised on instantiation.
2. You've hardcoded the account numbers. How would you cope if you had to add another 50 accounts?
But the problem you've reported is because you need to call the options function in a loop and keep looping until the user entered a valid option.