Computer Science Canada Bank Machine program |
Author: | Lundale34 [ Fri May 18, 2012 1:05 pm ] | ||
Post subject: | Bank Machine program | ||
What is it you are trying to achieve? I need my program to take out money in intervals of 20's up to 500. It will crash when anything but an interval of 20 or over 500 is inputted, it will also allow the program to take out more than the current balance. What is the problem you are having? <Program crashes> Describe what you have tried to solve this problem <Everything> Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) <procedure makeWithdraw1 var withdrawAmount : real drawfillbox (0, 0, 500, 200, blue) locate (20, 12) put "Enter in the amount to be withdrawn :" .. get withdrawAmount balance:=makeWithdraw (balance, withdrawAmount) locate (21, 20) put "Your current balance is :", balance end makeWithdraw1>
Please specify what version of Turing you are using <4.1.1> |
Author: | Tony [ Fri May 18, 2012 3:39 pm ] |
Post subject: | Re: Bank Machine program |
Lundale34 @ Fri May 18, 2012 1:05 pm wrote: Describe what you have tried to solve this problem <Everything> Clearly not. Have you tried mod? |
Author: | Lundale34 [ Sat May 19, 2012 11:15 am ] |
Post subject: | RE:Bank Machine program |
Yes I have, I have a function that I thought I attached as well. function makeWithdraw (balance : real, var amountWithdrawn : real) : real var newBalance : real drawfillbox (0, 0, 500, 200, blue) locate (20, 20) if amountWithdrawn mod 20 = 0 and amountWithdrawn <= 500 then put "You have withdrawn $", amountWithdrawn result balance - amountWithdrawn elsif amountWithdrawn > 500 then colour (white) put "Insufficient Funds" colour (blue) end if end makeWithdraw |
Author: | Aange10 [ Sat May 19, 2012 8:31 pm ] | ||
Post subject: | RE:Bank Machine program | ||
From running the program it seems like your problem is here:
The error message is "Function failed to give result". Try adding a result even if you didn't withdraw how much they asked. If you didn't withdraw anything, the result of the transaction would be the same as the balance before it. Also, the else clause's 'put' statement is incorrect, it's not insufficient funds, it's that the funds aren't incremented in twenty. |
Author: | Lundale34 [ Mon May 21, 2012 10:15 am ] |
Post subject: | RE:Bank Machine program |
I fixed the over 500 problem, but still have had no luck with the increments of 20. The function is now <function makeWithdraw (balance : real, var amountWithdrawn : real) : real var newBalance : real drawfillbox (0, 0, 500, 200, blue) locate (20, 20) if amountWithdrawn mod 20 = 0 and amountWithdrawn <= 500 and amountWithdrawn < balance then put "You have withdrawn $", amountWithdrawn result balance - amountWithdrawn elsif amountWithdrawn > 500 then colour (white) locate (20,20) put "Please enter an amount under $500" colour (blue) result balance elsif amountWithdrawn > balance then colour (white) put "Insufficient Funds" result balance end if end makeWithdraw> |
Author: | mirhagk [ Mon May 21, 2012 12:04 pm ] |
Post subject: | RE:Bank Machine program |
You need another else that gets run when amountWithdrawn mod 20 is not 0. It needs to have a result balance. In fact you could just move the result balance from the other 2 elsif blocks to the end of the function (after the end if). That will make it so that as long as one of the if statements doesn't return anything, it returns their balance. |
Author: | evildaddy911 [ Mon May 21, 2012 12:30 pm ] |
Post subject: | RE:Bank Machine program |
if you know [even extremely basic] buttons, then make some for each value that you can withdraw |
Author: | Raknarg [ Mon May 21, 2012 2:56 pm ] |
Post subject: | RE:Bank Machine program |
@evildaddy he needs the basics down before he adds anything nice to it. |
Author: | evildaddy911 [ Mon May 21, 2012 3:24 pm ] | ||
Post subject: | RE:Bank Machine program | ||
well, it seems that the problem has to do with dummy-proofing, and dummy-proofing is much easier without text input...
what do you see wrong with the 3rd line?[/syntax] |
Author: | Tony [ Mon May 21, 2012 3:55 pm ] |
Post subject: | RE:Bank Machine program |
fun fact (that you don't have to worry about for a while): if this code is running in parallel over multiple instances (e.g. there exists at least two ATM machines), then it's possible to withdraw $1000 from an account that started with a balance of just $501. 1st machine: amountWithdrawn == 500. less than balance (501). Check successful! 2nd machine: amountWithdrawn == 500. less than balance (501). Check successful! 1st machine: give out money. Balance == 1 2nd machine: give out money. Balance == 1 (or balance == -499, depending on how the variable is shared/synced) |
Author: | Zren [ Mon May 21, 2012 3:57 pm ] |
Post subject: | RE:Bank Machine program |
Quote: amountWithdrawn < balance I'm totally allowed to withdraw 20 bucks from an account with 20$ in it. |