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

Username:   Password: 
 RegisterRegister   
 Bug with Turing?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Fozmik




PostPosted: Tue Nov 14, 2006 8:38 pm   Post subject: Bug with Turing?

This is a program I've been working on for a while and it is supposed to give ideal change for a twenty dollar bill. It works great for most numbers except for when the price is like 1.05, 2.05, 3.05, 4.05, 5.05 etc, or amounts with cents in values of .10 - .30 and .70 - .90 (this is like $0.10, $0.20, $.30, $1.10, $1.90). Also it doesn't work for numbers ending in .98.

Something interesting to note is that the problem only occurs once it's finished giving change for tens, fives, toonies, loonies and quarters and starts doing dimes and downward. This is where things happen like the program gives 9 pennies instead of 10, and rather than giving 2 dimes it would give a nickel and 5 pennies (in some cases it falls short and gives 4).

To fix the problem I tried using the break function and output the Change variable amount every so often and sometimes the variable ends up being 0.05 and it is supposed to mod itself by 0.05 to end at 0 (and 0 pennies for exact change), but turing chooses to ignore this line and leaves the Change variable at 0.05. In the output window this looks like 5 extra pennies, and also not in the form of a nickel.

Is this a problem with turing or is it because I am using div and mod in the wrong places?

code:
setscreen ("graphics")
var price : real
var change : real
var change2 : real
var pennies : real
var answer : array char of boolean
var window : int
var finished : boolean := false
var location : boolean := false
var medarial : int
medarial := Font.New ("arial:12")
window := Window.Open ("position:center;center,graphics:250;260")
Font.Draw ("This program calculates the most", 0, maxy - 14, medarial, black)
Font.Draw ("ideal change for a twenty dollar bill.", 0, maxy - 34, medarial, black)
loop
    loop
        loop
            if location = false
                    then
                Font.Draw ("The total cost of the item(s)", 0, maxy - 74, medarial, black)
                Font.Draw ("purchased is:", 0, maxy - 94, medarial, black)
                Font.Draw ("$", 0, maxy - 122, medarial, black)
                locate (7, 2)
                get price
                location := true
            else
                Font.Draw ("The total cost of the item(s)", 0, maxy - 14, medarial, black)
                Font.Draw ("purchased is:", 0, maxy - 34, medarial, black)
                Font.Draw ("$", 0, maxy - 68, medarial, black)
                locate (4, 2)
                get price
            end if
            if price > 20
                    then
                cls
                Font.Draw ("The amount must be equal to", 0, maxy - 14, medarial, black)
                Font.Draw ("or smaller than $20.", 0, maxy - 34, medarial, black)
                location := false
            elsif price <= 20
                    then
                exit
            end if
        end loop
        cls
        if price = 20
                then
            Font.Draw ("No change.", 0, maxy - 14, medarial, black)
            exit
        else
            Font.Draw ("The cost is $", 0, maxy - 14, medarial, black)
            locate (1, 10)
            put price : 0 : 2
            Font.Draw ("Your change is $", 0, maxy - 32, medarial, black)
            locate (2, 13)
            put 20 - price : 0 : 2
        end if
        change := 20 - price
        if change >= 10
                then
            if change div 10 > 1
                    then
                Font.Draw (realstr (change div 10, 1), 0, maxy - 65, medarial, black)
                Font.Draw ("ten dollar bills.", 12, maxy - 65, medarial, black)
                change := change mod 10
            else
                Font.Draw ("1 ten dollar bill.", 0, maxy - 65, medarial, black)
                change := change mod 10
            end if
        else
            Font.Draw ("No ten dollar bills.", 0, maxy - 65, medarial, black)
        end if
        if change >= 5
                then
            if change div 5 > 1
                    then
                Font.Draw (realstr (change div 5, 1), 0, maxy - 85, medarial, black)
                Font.Draw ("five dollar bills.", 12, maxy - 85, medarial, black)
                change := change mod 5
            else
                Font.Draw ("1 five dollar bill.", 0, maxy - 85, medarial, black)
                change := change mod 5
            end if
        else
            Font.Draw ("No five dollar bills.", 0, maxy - 85, medarial, black)
        end if
        if change >= 2
                then
            if change div 2 > 1
                    then
                Font.Draw (realstr (change div 2, 1), 0, maxy - 105, medarial, black)
                Font.Draw ("toonies.", 12, maxy - 105, medarial, black)
                change := change mod 2
            else
                Font.Draw ("1 toonie.", 0, maxy - 105, medarial, black)
                change := change mod 2
            end if
        else
            Font.Draw ("No toonies.", 0, maxy - 105, medarial, black)
        end if
        if change >= 1
                then
            if change div 1 > 1
                    then
                Font.Draw (realstr (change div 1, 1), 0, maxy - 125, medarial, black)
                Font.Draw ("loonies.", 12, maxy - 125, medarial, black)
                change := change mod 1
            else
                Font.Draw ("1 loonie.", 0, maxy - 125, medarial, black)
                change := change mod 1
            end if
        else
            Font.Draw ("No loonies.", 0, maxy - 125, medarial, black)
        end if
        if change >= 0.25
                then
            if change div 0.25 > 1
                    then
                Font.Draw (realstr (change div 0.25, 1), 0, maxy - 145, medarial, black)
                Font.Draw ("quarters.", 12, maxy - 145, medarial, black)
                change := change mod 0.25
            else
                Font.Draw ("1 quarter.", 0, maxy - 145, medarial, black)
                change := change mod 0.25
            end if
        else
            Font.Draw ("No quarters.", 0, maxy - 145, medarial, black)
        end if
        if change >= 0.10
                then
            if change div 0.10 > 1
                    then
                Font.Draw (realstr (change div 0.10, 1), 0, maxy - 165, medarial, black)
                Font.Draw ("dimes.", 12, maxy - 165, medarial, black)
                change := change mod 0.10
            else
                Font.Draw ("1 dime.", 0, maxy - 165, medarial, black)
                change := change mod 0.10
            end if
        else
            Font.Draw ("No dimes.", 0, maxy - 165, medarial, black)
        end if
        if change = 0.05
                then
            if change div 0.05 > 1
                    then
                Font.Draw (realstr (change div 0.05, 1), 0, maxy - 185, medarial, black)
                Font.Draw ("nickels.", 12, maxy - 185, medarial, black)
                change := change mod 0.05
            else
                Font.Draw ("1 nickel.", 0, maxy - 185, medarial, black)
                change := change mod 0.05
            end if
        else
            Font.Draw ("No nickels.", 0, maxy - 185, medarial, black)
        end if
        if change >= 0.01
                then
            if change div 0.01 > 1
                    then
                Font.Draw (realstr (change div 0.01, 1), 0, maxy - 205, medarial, black)
                Font.Draw ("pennies.", 12, maxy - 205, medarial, black)
            else
                Font.Draw ("1 penny.", 0, maxy - 205, medarial, black)
            end if
        else
            Font.Draw ("No pennies.", 0, maxy - 205, medarial, black)
        end if
        exit
    end loop
    Font.Draw ("Press ' n ' to exit the program.", 0, maxy - (maxy - 3), medarial, black)
    Font.Draw ("Press ' y ' to receive change again.", 0, maxy - (maxy - 23), medarial, black)
    loop
        Input.KeyDown (answer)
        if answer ('y')
                then
            cls
            exit
        elsif answer ('n')
                then
            finished := true
            exit
        end if
    end loop
    exit when finished
end loop
Window.Close (window)

Sponsor
Sponsor
Sponsor
sponsor
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  [ 1 Posts ]
Jump to:   


Style:  
Search: