Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Bank Machine program
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Lundale34




PostPosted: 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>

Turing:


<Add your code here>



Please specify what version of Turing you are using
<4.1.1>



Unnamed.t
 Description:

Download
 Filename:  Unnamed.t
 Filesize:  2.53 KB
 Downloaded:  141 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Lundale34




PostPosted: 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
Aange10




PostPosted: Sat May 19, 2012 8:31 pm   Post subject: RE:Bank Machine program

From running the program it seems like your problem is here:

Turing:

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 then
        put "You have withdrawn $", amountWithdrawn
        result
            balance - amountWithdrawn
    else
        put "Insufficient Funds"
    end if
end makeWithdraw



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.
Lundale34




PostPosted: 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>
mirhagk




PostPosted: 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.
evildaddy911




PostPosted: 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
Raknarg




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
evildaddy911




PostPosted: 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...

Turing:
if amountWithdrawn mod 20 = 0 and amountWithdrawn <= 500 and amountWithdrawn < balance then
put "You have withdrawn $", amountWithdrawn
result
balance - amountWithdrawn
elsif ...

what do you see wrong with the 3rd line?[/syntax]
Tony




PostPosted: 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)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Zren




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: