Computer Science Canada

Variable has no value!?

Author:  tristanbryce7 [ Wed Oct 10, 2012 3:57 pm ]
Post subject:  Variable has no value!?

Hey guys, I have a problem in Turing, I'm new to Turing and just started learning it so yeah. So I made a program that is like a menu, and you have 5 options, and you enter a number for the item you want, and than it asks u how many u want of that , and than calculates the cost.
This is the code for it
__________________________________________________________________

var food : string (1)
var burgeramount : real
var popamount : real
var friesamount : real
var applepieamount : real
var milkshakeamount : real
var Cost : real


loop
put "Hello, Please place your order from the following options"
put "1. Burger = $2.50"
put "2. Pop = $1.50"
put "3. Fries = $1.60"
put "4. Apple Pie = $2.70"
put "5. Milkshake = $3.15"
put "6. Done food Selection"
put ""
put "Put Item Number Over Here ==>" ..
getch (food)

case food of


label "1" :
put "How many burgers would you like?"
get burgeramount
cls
label "2" :
put "How much pop would you like?"
get popamount
cls
label "3" :
put "How many packs of fries would you like?"
get friesamount
cls
label "4" :
put "How many Apple Pie would you like?"
get applepieamount
cls
label "5" :
put "How many milkshakes would you like?"
get milkshakeamount
cls
label "6" :
cls
Cost := (2.5 * burgeramount) + (1.5 * popamount) + (1.6 * friesamount) + (2.7 * applepieamount) + (1.5 * milkshakeamount)
put "The Bill is $", Cost, " !"

label :
put "Please put a number from 1-6"
delay (1500)
cls

end case
end loop

_____________________________________________________________________

So my problem is , if the person running the program doesnt select all 5 of the items in his selection, it says "variable has no value" . WHich i get, it means since person didn't enter anything for say burger, it wont have anything , so my question is, how do i change the code so that if they dont choose, say, pop and burger, but the other items, the program calculates the cost fine and theres no problem, so how would i change to code to do that ?

Author:  mirhagk [ Wed Oct 10, 2012 4:10 pm ]
Post subject:  RE:Variable has no value!?

Change

Turing:
var burgeramount : real
var popamount : real
var friesamount : real
var applepieamount : real
var milkshakeamount : real


to
Turing:
var burgeramount : real :=0
var popamount : real :=0
var friesamount : real :=0
var applepieamount : real :=0
var milkshakeamount : real :=0


So that they equal 0 by default. That should fix your problem. You need a starting value if you're going to ever use it without it being 100% defined beforehand.

EDIT: For future reference you can use [syntax="turing"] your code here [ / syntax] (but without the spaces) to have the forum use syntax highlighting and put it in a nice code block (like above)

Author:  tristanbryce7 [ Wed Oct 10, 2012 4:14 pm ]
Post subject:  Re: Variable has no value!?

Thank you so muchh!!! Yea, that fixed the problem Smile, It'll help me a lot in the future too Very Happy

Oh, ok , Sorry, I never knew that lol, my first post on the website, ill try to do it next time

Author:  mirhagk [ Wed Oct 10, 2012 7:41 pm ]
Post subject:  RE:Variable has no value!?

No problem, welcome to the site. So long as you keep that in mind for your next post we're happy to help.

The code tags will get you help faster because people can look over it quickly and see mistakes. Many people actually skip a question that doesn't have code tags, but I for one look to see if the person is within their first few posts, and will try to help out regardless.

Anyways, if you ever need help just come here, it's a great place to get help, to discuss topics once you get advanced enough and to just converse and ask about university and stuff. Welcome.

Author:  tristanbryce7 [ Thu Oct 11, 2012 3:55 pm ]
Post subject:  Re: Variable has no value!?

Thanks , it means a lot lol ,
yea, once i get into advanced languaging, i could try and converse with the people who know what theyre doing Very Happy


: