Skip to main content
GameDev.net gamedev.net
🔒 Locked

BASIC Help

Started by CPF Apr 16, 2007 at 4:23 PM 7 replies 2.2k views
Original Post
CPF
CPF
Hi, I'm 17 and just got "Game Programming for Teens 2nd Ed." I'm having trouble converting variables. Just began experimenting with arrays and functions. I am trying to get a variable to return as a floating point variable. I know this is most basic of (no pun intended) basic, but any help for a noob is apreciated.
_Sigma
_Sigma
It has been ages since I've done any BASIC. Posting some of your code might help in narrowing down the issue.

In programming, you have to be aware of types. That is, you can't blindy take a floating point value, 3.33 for example and turn it into an integer. Integers, by definition, cannot hold decimals, in this case, the .33.

So, you can convert values, you just need to pay attention to what you are doing. Now lets say you really did want to covert a value. Something like this should work (from MSDN)
Dim value As DoubleDim returnValue As IntegerreturnValue = Convert.ToInt32(value)


as for a function
Function myFunction(ByVal j As Integer) As Double    Return 3.87 * jEnd Function

Will return a double (that is, something that can hold a decimal point)

These examples came from the MSDN site, and so I assumed you were using Visual Basic (.NET)

hope this helps.
c0d3s33k3r
c0d3s33k3r
Lol i just got the book to, blitzcc is pretty difficult.

try this variable cast method,

firstvar$ = secondvar$ ; like i said, i just got the book and started the language, im using cast methods i learned from visual basic 6 enterprise edition.

c0d3s33k3r
c0d3s33k3r
c0d3s33k3r
Lol i just got the book to, blitzcc is pretty difficult.

try this variable cast method,

firstvar$ = secondvar$ ; like i said, i just got the book and started the language, im using cast methods i learned from visual basic 6 enterprise edition.

c0d3s33k3r
diablo_tk
diablo_tk
in blitz to return a floating point variable use the # sign ..

function myfuntion#(arg1#)

return arg1#

end function
website: fallenmoon.netHowdey Hoe
_Sigma
_Sigma
Whoa ... we aren't talking about BASIC? O_o
CPF
CPF
It might help if i clarify a bit. I'm using a demo BASIC compiler called BlitzPlus. Where I'm at in the book arrays have just been introduced. I made a simple calculator, but it is unable to return decimal points.I have tried renaming the variable with # and it workes for the pieces of the problem but not the solution. Here is the original code - taken from book - that i have been trying to edit to use decimals.

Global op1
Global op2
Dim array(100) ;0-100
InitializeArray()

continue = 1
While continue

op1 = Input("What is the first number")

op2 = Input("And the Second?")

operator$ = Input("Enter +, -, *, or / ")

PrintAnswer(operator$)

continue = Input("Enter 1 to continue or 0 to quit ")

Print""
Wend
End

Function InitializeArray()
For i=0.00 To 100.00
array(i) = i
Next
End Function

Function PrintAnswer(operator$)
Print op1 + " " + operator$ + " " + op2 + " is equal to " + FindAnswer(operator$)

End Function

Function FindAnswer(operator$)
Select operator
Case "+"
Return array(op1) + array(op2)
Case "-"
Return array(op1) - array(op2)
Case "*"
Return array(op1) * array(op2)
Case "/"
Return array(op1) / array(op2)
End Select
End Function
diablo_tk
diablo_tk
I havent used plus in ages but I think if you just change the first two lines...

Global op1
Global op2

to

Global op1#
Global op2#

It should work. If not I'll have a closer look when I get home.

btw, what variable did you put a # on the end of?

:::::: Edit ::::::::

Forget that. Its beacuse you are useing an array to lookup the numbers.

i.e.

Function InitializeArray()
For i=0.00 To 100.00
array(i) = i
Next
End Function

... arrays are indexed using intergers so you cant use floating point numbers, for example array(1.5) isnt vaild even though they can store float point numbers.

The only way you can get a floating point anwser is to remove the array part in FindAnswer (also make it return as float)

Function FindAnswer#(operator$)
Select operator
Case "+"
Return op1 + op2
Case "-"
Return op1 - op2
Case "*"
Return op1 * op2
Case "/"
Return op1 / op2
End Select
End Function
website: fallenmoon.netHowdey Hoe
CPF
CPF
Thanks that helped. I got it working now!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.