ATM Cash Machine in C++
The best part of being a game dev to me is...well, making games. So much can be done without touching a line of code, which is great. Well, not really. Recently, I have made sure to implement more code (C++ and C#) into my projects but I feel like I'm neglecting my programming.
So I decided to boot up some exercises and test myself with creating a cash machine/ATM interaction system in C++.
The first step was to set up the functions and then define the variables. I then created the UI and printed out what would be the interface and then allowed the user to input their choice. I wrapped that in a do while loop and created switch cases for each choice presented in the UI. I then initialised the functions for later use.
Then the output presents the user with the choices for input. (Yes every option is 1 in the UI for now, don't worry I catch that error later!)
If the user presses 4, they will exit the interaction.
To show the balance, I printed out the balance and added a setpresicion (using <imanip> library) to allow for inputting decimal points.
Then the output log will show the user their balance (for this case I set the default to $123)
The next step was to implement the deposit function. I initialised the amount as 0 and allowed the user to input their amount.
This then displays the deposited amount in the output. The user can deposit more money and check their new balance.
I then made sure the user could not input anything less than zero to avoid inputting minus values.
Next is the withdraw function and the code is very similar to the deposit function. The user is able to input their choice to withdraw.
The user can then withdraw their inputted number and at this stage they can check their balance and deposit and withdraw money.
Like in the deposit function, I needed to make sure the user couldn't withdraw more money than they have in their balance. This was achieved by checking that if the amount was greater than the balance, the user could not withdraw more money.
Okay, there is a problem that still needs to be fixed. If the user inputs a character, well the code doesn't have fun! No matter, should be an easy fix.
Comments
Post a Comment