Learn Programming in C - Lesson 18 - ATM

Learn Programming in C – Lesson 18 – ATM

Views:46837|Rating:4.84|View Time:19:17Minutes|Likes:273|Dislikes:9
In this video we focus on a programming project – a more complete ATM machine. This project will culminate everything we’ve talked about before.

Next Video: (Comming Soon)
Previous Video:

First Video:

this video is actually more of a culmination of all the stuff we've done in the past it's actually going to be an ATM machine and I know we've done an ATM machine in the past but this time we're going to bring in all of the stuff that we've learned over the last sets of videos if you're completely comfortable with everything we've done over the last videos then you can skip over this and move on to the next one but if you'd like to see kind of all of the the elements that we've learned brought into one program our ATM machine then just follow along and maybe you should write it yourself as well the ATM machine has a bunch of requirements up obviously and these requirements are that it's going to have a pin and you can only use the ATM machine if you have this pin entered of 1 2 3 1 if you don't enter the right pin and won't let you access the ATM you can also obviously check your balance you can see how much money is in your account you're allowed to deposit money into the ATM and that will increase your balance but you must deposit a positive amount can't deposit a negative amount that would cause some issues you also can withdraw money which will subtract your balance and you can only withdraw a multiple of 20 because our ATM machine only has 20 dollar bills you must have enough money in your account to take out the money and you can perform multiple transactions that's the last requirement so as you are using ATM machine you are able to continually access multiple transactions and manipulate your balance so let's jump right into our program here and what we have so far is we have our balance variable notice as int so this means that our balance here is actually going to be whole number so we're just going to keep it simple with whole dollar values no cents or any kind of subdivision there so right away we can start with our pin and we need a variable to hold our pin so let's create an int and it's just going to contain our current pin so this is makes it easy for you to change it later on so we're gonna make it one two three one right so we have our pin in here and whenever we're logging in to our ATM we have to ask the user to enter in this pin and we'll verify that they entered the correct one so let's actually do that let's ask them for the pin so we're gonna ask them for so we've asked them and now we need to read it in so we are going to scanf an integer but we need another variable so we're gonna create a new variable up here called tenpin and this is just going to be the pin that day I've entered so obviously we need or an ampersand is normal and we have our tenth pin so now we need to do our little check here so instead of app checking if it's valid what we're going to do is actually check if it's invalid and in if it is invalid we're just gonna leave the program telling them that it's the wrong pin so let's do that so we're gonna see if the pin doesn't equal tempt in great so if it isn't equal to the right pin we're just gonna boot them right out we're just gonna be like invalid pin and not even give them a new shot we are going to return right away and just leave the program so we're gonna leave this main yeah by returning out of it earlier than we would normally so this will just actually just kick right out and the program great now we can actually work with our balance at this point and at this point we need to ask them what they want to do because remember that they can either check the balance deposit or withdraw so we need to ask them what they want to do so we're gonna go and ask them what do you want to do and we're gonna go them a few options with some numbers that are going to correspond to those options so we'll go like this we'll go one is going to be check your balance your balance and then we're gonna do two posit and we're gonna do three withdraw great so we have our options here now we need to ask them what option they want to do now we're gonna have a variable here called option an option is going to have the option they chose so we'll do a scanf and we'll read in that value great so we have this option now and now we can actually react to what they've set so we need an if statement and our if statement is going to be if the option is equal to one at this point we just need to tell them your balance so we can say your balance is and then give them a dollar sign and then we can give them a balance variable that holds our balance great now we have another case and we're gonna make it an else if so that way it's only gonna do one or the other and then we will say if the option is not equal to one but it's actually equal to two so this means that they are going to deposit I'm gonna put a little comment beside it just to say that we know what it is so we're gonna say then check balance great so now we're gonna deal with the deposit and we need to ask them how much money so we need your variable to say amount that we are going to deposit like I spell deposit and we need to ask them and for now we're not actually going to do any kind of checking we're gonna add that in later let's just get the basic program happening then we can do add in our checks to make sure that they're adding their depositing a positive amount and we're all good so let's for now just read in our about amount here so we're gonna read in amount deposit adding our ampersand and we're going to make the deposit so we need to add it in so we're gonna your balance is equal to balance plus our amount deposit or we can do our short form which is take away the balance and put the plus before which we'll add it into two pots into balance so that will just increase the amount of our balance by the amount deposit now we just need to do our option equal to 3/4 I withdraw great so now we can do our withdraw and to do the same thing we're gonna actually do pretty much identical to our deposit we're gonna ask for the amount withdraw how much money do you want to withdraw so it's pretty much identical to the previous the only difference in this case because we're not adding our checks in yet is that we're going to subtract the amount instead of adding the amount so we'll do the same type of thing I should put the bracket and we're gonna go balance would be subtracted by the amount withdraw great so now we have those options but last but not least we need to check to make sure that they enter a valid option and so far that we have our if for if it's equal to one if it's equal to two and if it's equal to three but we need to just throw in a catch-all else to make sure that if they entered something else we can say in a valid transaction so they chose something that they're not supposed to so if we actually run this program let's see if we made any mistakes at all so let's compile it and we call a UTMC and let's see it compiled correctly but let's see if it runs so we're gonna enter our pin and let's give it an invalid pin to start so let's go for four four and look at that it kicks us out it goes for this if statement and it says if they don't equal print this out and just leave and so that's what it did so let's give it a let's give it a valid pin here so one two three one and we're in so now we can actually use our menu here and see if we for example can check our balance so let's see see what happens when we check our balance and oh no our balance is actually something like negative 1 billion and so why is that why is it just something random and the reason is is because we haven't actually set our balance to anything and it's using whatever is in memory right now at that location wherever decided to put balance so what we need to do is actually initialize balance to zero so when it creates the variable it'll set it to zero so if we run it again and check our balance we'll get a balance of zero and if we run it again and deposit and so we deposited 40 but they didn't do anything it just left so we don't actually know if it deposited or if it affected our account and next time when we ran it it'll wipe out our balance again and give us a balance of zero so what what we need to do is actually add that last requirement in to allow multiple transactions and so this means that we actually place all of this transaction stuff within a loop that we can do multiple times so we need to actually put this into a while loop and so we're gonna put a while around all of this transaction and now that we have this while loop let's actually indent all of this so that way we know it's part of the while loop and in this editor if you select text and hit tab it actually indents at all so then what's our condition well we're going to need to store if we want to do another transaction and so to do this we're gonna need a new variable and the new variable we'll call another transaction and it's gonna use one to represent do another transaction and two to represent no not another transaction so we've decided that this variable is going to determine if we want to do another transaction so what we'll do here in this case is actually go another transaction and of course we want to check if another transaction is equal to one so we need a double equals to check if they are equal and if they are equal which in this case they will be because another transaction is equal to one when it's initialized it'll enter our loop and if we read it right now it would work but the only thing is it would never leave it would always be equal to one because we never affected it so what we need to do is actually ask them if they want to do another transaction so we're gonna ask do you want and of course they need to know that one is equal to let me put my new line oops one is equal to yes and two is equal to no then we need to take our another transaction and read in our value to that variable so with our ampersand in place we can go to another transaction so now we'll read in our variable and it will read into another transaction and it will repeat as long as another transaction E is equal to one but what would happen if they entered a number other than one well it would leave but we want to try and restrict the user to either entering in one or two we don't really want them to enter in three so what we can actually do is wrap this again in another loop so we'll set another transaction here equal to zero an invalid value and then what we'll say is while another transaction is equal or doesn't equal 1 and another transaction doesn't equal 2 then we wanted to perform this code which as long as another transaction is not valid which it starts out not being valid at this point then it will keep repeating this until they either enter in 1 or 2 at that point it will leave and then this big loop determines if they want to actually do another transaction so now we have a nested loop and this will ask them it for a valid response because we should do our checking and then once they have a valid response depending on that value it'll either repeat or we'll leave the loop and exit the program so let's try it out and make sure that we are looping correctly 1 2 3 1 your check deposit it's 1 do you want to perform another transaction yes we do we can deposit will deposit a 40 and we will do another transaction and check that it is indeed 40 and finally we'll do a withdraw of $20 and we will with we will do another transaction to check that it is indeed now 20 great no I should have hit 2 – we'll leave the program great I'm gonna add that new line and now we have our looping so we're almost done or our fantastic ATM machine we just need to make sure that our deposit and our withdraw is now validating that they entered right value so that they didn't enter negative for a deposit and they they have our two validation for withdrawing so first we'll do the easy check and we will go like this if the amount deposit is greater than zero so it's a valid amount then we will do the deposit if it's not a valid about otherwise they entered in zero or negative number then you can say invalid deposit amount so now we have that done and and it'll check to make sure that's a valid value so if we if R we compile and run our program again let me just clear out the screen here we enter in our pin and we will deposit a negative amount it says invalid deposit amount and then it just asks do you want to do another transaction which is perfectly fine so now we need to just check our withdraw amount and this can have a few cases so we want to do a few things we want to check that if they have enough money so that means that the amount to withdraw is less than or equal to less or equal to the amount that they have which is about so if they have they're withdrawing less money or equal to the amount that they have they can take it out and that the amount to withdraw is divisible by 20s we're going to do our mod 20 and if the remainder of the dividing of 20 is equal to zero then they can do it and at that point yes you can withdraw the amount otherwise in this case we should give them two separate error messages so they kind of know what's from that what's right and then we can say if the amount withdraw is greater than the balance at that point we can give them the error message that you don't have enough money declined which is always a fun feeling and then lastly if we know that they weren't declined because of the amount to withdraw then we know what the other condition failed and that means that you must enter an amount that is divisible by 20 okay so now we have a lot of checks in here and so now our ATM machine hopefully is complete let's try it out and will deposit a negative amount it'll fail we'll then withdraw something like let's try 200 doesn't have enough money so let's put some money in there so we will check our we will sort deposit $500 then we will check our balance to make sure that's 500 then we'll do a little let's withdraw let's try it 50 and it will say no it has to be divisible by 20 but we can for sure withdraw 60 and then we'll check our balance to make sure that it is for 40 good so it looks like our ATM machine is working perfectly fine so now we can finish by entering 2 to not do another transaction and there is our complete ATM machine next video we're going to talk about something called functions and it'll let us actually clean up this program so that way we don't have all of this stuff in one big loop

26 Replies to “Learn Programming in C – Lesson 18 – ATM”

  1. kirollos magdy

    I have a question , if i" withdrawl " 1000$ is the machine going to give me :
    2 * 500 $
    or 1 * 500$ and 1*100$ and 2*200$
    or 10*100$
    it is very confusing that the machine almost never gave me the same number of papers , so why is that ?

  2. All In One

    I am doing a ms visual studio c# atm integrated project and i need to handin by the 5th of jan, 2017. Can you help me plz? there is 2 forms, one is login form(when i press submit i go to the other page which i deposit, withdraw, transfer funds, and pay bill, keypad.

  3. Abe Hariz

    Hello , fell great to learn C progamming on your channel.
    But , hope you still updating this channel soon.
    Thank you 🙂 .

  4. Michael Short

    An easier way to handle the anotherTransaction loop would be to have the anotherTransaction variable equal to either 1 for yes or 0 for no.  C by default interprets 0 as false and anything above 0 as true.

    This way the loop would be as simple as

    while(anotherTransaction) {
    // code here

    and there wouldn't be any need for the inner loop if the user entered an invalid option for anotherTransaction.

  5. Conwear TheWind

    Hi you guided me through my week with your tutorial series and i would love to know if you will be continuing it or if you quit making it.

    But you have done a good job till now!

  6. Alexander Faitelson

    You also should check 
    if(amountWithdraw > 0)
    because now I can deposit 100 and then withdraw -80 and my balance will be $180 ))

  7. Braden Best


    Quick Tip: If you know a piece of information is never going to change in the program, then it's better to use a constant than a variable.

    This would apply to maximums, minimums, etc.

    Maybe you are designing an open-source program that you want to be configured via altering the source code and recompiling (this is done in the OSS community more often than one might think). In that case, you would definitely want to use constants.

    For example, in an earlier video, when I was following the ATM example, rather than hard-code the "max quota" into the if statement

    if(money < 1000)

    or use a variable to hold it

    int money = 1000

    I used the precompiler directive, define, to make a constant

    #define MAX_QUOTA 1000

    if(money < MAX_QUOTA)

    Edit: hashtags? Really?

Leave a Reply

Your email address will not be published. Required fields are marked *