Does not div/mod properly...
Author |
Message |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Thu Apr 17, 2008 8:21 am Post subject: Does not div/mod properly... |
|
|
This is the 'vending machine' program that I am doing for my grade 10 computer engineering class. However, It says things like '5 quarters' and '7 loonies'. This isn't supposed to happen! Could you tell me what's wrong and why, so that I can fix it and know what I did wrong?
code: |
%********************************************************************
% Jordan Dykstra *
% April 16 *
% This program allows the user to order a candy bar and recieve *
% change as required *
%********************************************************************
%***********************************
% Variables
var money : int % The ammount of money put into the machine
var change : int % The remaining change
var total : int % The total cost of the item
var item : string (1) % The item (getch'ed from user)
var twent : int := 2000 %*\
var ten : int := 1000 %****\
var five : int := 500 %*****\
var toon : int := 200 %******\
var loon : int := 100 %*******\ setting the value of coins
var quart : int := 25 %*******/ Like the fancy slashes???
var dime : int := 10 %*******/
var nick : int := 5 %*******/
var penn : int := 1 %******/
var totaltwent : int %\
var totalten : int %*\
var totalfive : int %*\
var totaltoon : int %**\
var totalloon : int %***\
var totalquart : int %***\ The number of coins
var totaldime : int %****/
var totalnick : int %***/
var totalpenn : real %*/
process PlayMusic
Music.PlayFile ("cd")
end PlayMusic
process other
%*****************************************
%Draw a box to outline machine
Draw.Line (100, 350, 500, 350, green)
Draw.Line (100, 100, 100, 350, green)
Draw.Line (500, 100, 500, 350, green)
Draw.Line (100, 100, 500, 100, green)
%*****************************************
%*****************************************
% Calculate cost of item *
%*****************************************
put "Enter Item code"
getch (item)
if item = "a" then
total := 125
elsif item = "b" then
total := 150
elsif item = "c" then
total := 75
elsif item = "d" then
total := 175
elsif item = "e" then
total := 250
elsif item = "f" then
total := 225
else put "Invalid option. you fkd up the program!"
end if
%*****************************************
put " please insert coin or bill"
put "we do not accept 50 or 100 dollar bills"
get money
money := money * 100
change := money - total
totaltwent := change div twent
change := change mod twent
totalten := change div ten
change := change mod ten
totalfive := change div five
change := change mod five
totaltoon := change div toon
change := change mod toon
totalloon := change div loon
change := change mod loon
totalquart := total div quart
total := total mod quart
totaldime := change div dime
change := change mod dime
totalnick := change div nick
change := change mod nick
totalpenn := change
put "Your change is:"
if totaltwent > 0 then
put totaltwent, " twenties"
end if
if totalten > 0 then
put totalten, " tens"
end if
if totalfive > 0 then
put totalfive, " fives"
end if
if totaltoon > 0 then
put totaltoon, " toonies"
end if
if totalloon > 0 then
put totalloon, " loonies"
end if
if totalquart > 0 then
put totalquart, " quarters"
end if
if totaldime > 0 then
put totaldime, " dimes"
end if
if totalnick > 0 then
put totalnick, " nickels"
end if
if totalpenn > 0 then
put totalpenn, " pennies"
end if
end other
fork other
fork PlayMusic
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
CodeMonkey2000
|
Posted: Thu Apr 17, 2008 3:18 pm Post subject: RE:Does not div/mod properly... |
|
|
Processes? Why the heck are you using a process for the main program? It should be by itself. And as for you question, code: | change := change mod twent | Does this make any logical sense? Wouldn't it make more sense if you did code: | change := change - twent*totaltwent | ? |
|
|
|
|
![](images/spacer.gif) |
Sean
![](http://compsci.ca/v3/uploads/user_avatars/47413941748406f441f83e.png)
|
Posted: Thu Apr 17, 2008 3:26 pm Post subject: Re: Does not div/mod properly... |
|
|
Also, CodeMonkey is right with what he says, however, Never fork, it's only necessary to do so when using Music. There is not need for the Main Program Process. |
|
|
|
|
![](images/spacer.gif) |
CodeMonkey2000
|
Posted: Thu Apr 17, 2008 3:31 pm Post subject: RE:Does not div/mod properly... |
|
|
Oh wait, the mod is right, but there is another problem. code: | totalquart := total div quart
total := total mod quart |
Total should be change. ![Razz Razz](http://compsci.ca/v3/images/smiles/icon_razz.gif) |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Fri Apr 18, 2008 8:04 am Post subject: Re: Does not div/mod properly... |
|
|
Doh! It's stupid mistakes like these that really get me...to bad I can't just use the project I did for computer science (The classes overlap...a lot...). I got 113% on that one...
My teacher sys I am supposed to use div/mod for this (I made a similar one using subtraction, but he rejected it
The totaltwent := change div twent bit is supposed to calculate the number of that particular coin/bill denomination
The following line, change := change mod twent calculates the remaining change.
Thanks for helping! Now I have to see if that fixes it... |
|
|
|
|
![](images/spacer.gif) |
|
|