Author:  Hunter007 [ Wed Jan 14, 2004 6:27 pm ]
Post subject:  Looping/Valid/Invalid Input...

I've been working on a lot of different progs at the same time (school) and I seem to have gotten confused. This program here is supposed to calculate any amount of pizzas(any size) with any number of toppings. No tax is needed. I think it calculates one order at a time now. I;m posting my code, hoping someone can help me out.

 code: const smallpiz : real := 6.00 const largepiz : real := 8.00 const smalltop : real := 0.75 const largetop : real := 1.25 var pizza : string var numtop : real var cost : real := 0 var topcost : real:=0 var reply : string (1) var totalcost : real:=0 var subtotal : real:=0 loop     put "Press any key to continue or 'q' to quit..."     put ""     getch (reply)     exit when reply = "q" or reply = "Q"     loop            put "**When finished type in 'none' as pizza size.**"            put""            put "Small or large pizza? " ..            get pizza            put ""            if pizza = "none" then                   exit            end if            put "Number of toppings: " ..            get numtop            if pizza = "small" then                   topcost := numtop * smalltop                   cost := smallpiz + topcost            elsif pizza = "large" then                   topcost := numtop * largetop                   cost := largepiz + topcost            end if            cls     end loop     totalcost := totalcost + cost     put "The cost of your pizza is \$", totalcost : 2 : 2, "."     delay (2500)     cls     cost := 0     topcost := 0     totalcost:=0 end loop

Author:  poly [ Wed Jan 14, 2004 7:47 pm ]
Post subject:

I see what your error is and its a very simple one, just have to move a line up. Here I posted the code below, you can see what I moved to fix this error, I made comment:

 code: const smallpiz : real := 6.00 const largepiz : real := 8.00 const smalltop : real := 0.75 const largetop : real := 1.25 var pizza : string var numtop : real var cost : real := 0 var topcost : real := 0 var reply : string (1) var totalcost : real := 0 var subtotal : real := 0 loop     put "Press any key to continue or 'q' to quit..."     put ""     getch (reply)     exit when reply = "q" or reply = "Q"     loop         put "**When finished type in 'none' as pizza size.**"         put ""         put "Small or large pizza? " ..         get pizza         put ""         if pizza = "none" then             exit         end if         put "Number of toppings: " ..         get numtop         if pizza = "small" then             topcost := numtop * smalltop             cost := smallpiz + topcost         elsif pizza = "large" then             topcost := numtop * largetop             cost := largepiz + topcost         end if                 totalcost := totalcost + cost   % I JUST MOVED THIS LINE BEFORE THE CLS         cls     end loop     put "The cost of your pizza is \$", totalcost : 2 : 2, "."     delay (2500)     cls     cost := 0     topcost := 0     totalcost := 0 end loop

the formula " totalcost := totalcost + cost " was after the CLS, so when it was time to tally it all up the CLS would just clear all vars. So I just moved it above the CLS and presto!

 Author: Thuged_Out_G [ Thu Jan 15, 2004 10:36 am ] Post subject: cls only clears the screen, it doesnt clear variables

 Author: DanShadow [ Thu Jan 15, 2004 1:47 pm ] Post subject: No, no, no...poly is right, and he wasnt clearing variables. Hunter007 wrote: cls end loop totalcost := totalcost + cost poly wrote: totalcost := totalcost + cost % I JUST MOVED THIS LINE BEFORE THE CLS cls end loop See, poly moved the totalcost calculator from outside the loop, to inside above the cls...he wasnt try to stop cls from clearing variables or anything. Author: Thuged_Out_G [ Thu Jan 15, 2004 2:52 pm ] Post subject: thats just the mesasge i got from poly wrote: CLS would just clear all vars

 :