Help needed in ATM programming

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;

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();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/ 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;

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 
waqqassheikh, that was unhelpful.

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.
Topic archived. No new replies allowed.