Can anyone help me figure out how I would do the withdrawals in multiples of 20?
The modulus operator springs to mind;
1 2 3 4 5
|
while ((value2 % 20) != 0)
{
cout << "You can only withdraw in increments of 20." << endl;
cin >> value2;
}
|
Other comments;
- your function prototypes could have had meaningful variable names in them:
double deposit(double amountToDeposit);
, for example.
- your functions both accepted two parameters, but only actually used one of them
- your variables could have had better names
double amountToDeposit, amountToWithdraw
for example
- Why
double
? This being an ATM, the
int
type is begging to be used.
-
system("pause");
is expensive, dangerous and non-portable.
- You don't keep track of the total amount. If a user deposits 100, and then deposits 100 again, and then again, and again, every time they will be told they have 600 in the account.
- Looks like the mark scheme expected some kind of on-screen confirmation of the amount the user withdrew, not just how much was left.
- Your code layout is not so neat as it could (and should) be. Instead of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
if(choice == 1)
{
cout << "How much money would you like to deposit?" << endl;
cin >> value1;
}
if(choice == 2)
{
cout << "How much money would you like to withdraw?" << endl;
cin >> value2;
}
if(choice ==1)
{
result = deposit(value1,value2);
}
else if(choice==2)
{
while(value2 ==20.0)
{
cout << "You can only withdraw in increments of 20." << endl;
cin >> value2;
}
result = withdraw(value1,value2);
}
|
this would have been better
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
if(choice == 1)
{
cout << "How much money would you like to deposit?" << endl;
cin >> value1;
result = deposit(value1,value2);
}
if(choice == 2)
{
cout << "How much money would you like to withdraw?" << endl;
cin >> value2;
while(value2 ==20.0)
{
cout << "You can only withdraw in increments of 20." << endl;
cin >> value2;
}
result = withdraw(value1,value2);
}
|
- This is completely useless, does nothing except clutter up the code
1 2 3 4
|
else if(choice ==-1)
{
//Exit Menu
}
|
I'm guessing it's there purely to stop this code being hit:
1 2 3 4
|
else
{
cout << "Please enter a valid option \n" << endl;
}
|
There are ways to do this without having a do-nothing branch.
- It seems that you only get to find out how much is in the account if you enter a value greater than zero or less than 3. In expected operation, this is when someone has chosen 1 or 2, and AFTER they've withdrawn/deposited. It would have been perhaps more sensible for an ATM to tell the user how much money they have
before they withdraw/deposit.
1 2 3 4
|
if(choice >0 && choice <3)
{
cout << "You have $" << result << " left in your bank account" << endl;
}
|
- Could have done with some handling of what happens if the user wants to withdraw more than they have in the account.