what is wrong

Started by
15 comments, last by eXtremeMachine 18 years, 10 months ago
Quote:Original post by eXtremeMachine
thanks thats the stupidest mistake i could have done.
I love goto's, but there's a time and a place for their power. I would use a do while loop for this program. OF course, FreeBasic calls it a do...loop.
let a=0let x=0let test = 1;Print "game"Print "Press any key to continue"WHILE INKEY$ = ""  X=RND() * 100wendclsDO   input "What is the number"; a  If x = a then    print "yes, your number is"; a    test = 0;  end if  elseif x < a then    print "Too high, your number is"; a  end if  else    print "Too low, your number is"; a  end ifLOOP UNTIL test = 0sleep


Notice, I switched your if statments to if, elseif, else statements. It's slightly quicker way and a bit more readable.
Advertisement
thanks i forgot about the do loop. and it is much more logical
any way the compiler did not reconise else, so i removed them and added a if then statement. also you did not need to and the ; for test=1

i am creating a high score, there is the save command and load but how do i use it
http://www.freebasic.net/keywords.php#31
The FUNCTION and SUB commands would be used to prevent writing a vital piece of code in more than one place.

FUNCTION
Imagine that you wanted to pick a new random number in more than one spot in your program. Instead of typing:

X=RND() * 100

in every place that you wanted to do so in your program, you could put the following code somewhere in your program once:

function NewNumber ()   NewNumber = RND() * 100end function


and then every time you needed a new number, you would just put

X = NewNumber ()

This will go to where you defined NewNumber () and execute the code you put in between the function NewNumber () and end function. Once it is done there, it goes back to the X = NewNumber () and replaces NewNumber () with the result of the function.

The execution goes something like this:

X = NewNumber () 'goes into the NewNumber () function you defined.
-> NewNumber = RND() * 100
-> NewNumber = 42 '42 is just an example. Now it goes back to:
X = NewNumber () and replaces the NewNumber () with NewNumber's result, 42.
X = 42

Why bother with this?It may not make much sense to use this in your current program. Now, imagine having needing to get a new number in many places throughout your program. What if you decided to change the way you came up with random numbers? Let's say you now want the number to be from 1 to 1000.

Without FUNCTION, you'd have to find every place you put X = RND() * 100 and change it to X = RND() * 1000.

With FUNCTION, you could just go to where you put:

function NewNumber ()   NewNumber = RND() * 100end function


and replace it with:

function NewNumber ()   NewNumber = RND() * 1000end function


You would only have to change it once in your program, minimizing the chance that you're going to forget to make a change in one place.

SUB
The reasons for using SUB are the same as for using FUNCTION. The only difference is how it's used, since SUB's do not return anything. You would not be able to do X = NewNumber () with a SUB because SUBs are not allowed to return anything.

Instead, you would use it like this:

sub NewNumber ()   X = RND() * 1000end sub


And every time you wanted a new number in your program, you would just use NewNumber. For example:

WHILE INKEY$ = ""NewNumberwend



I have no experience with FreeBasic, but I hope this helps you. Hopefully my syntax is correct. If you still do not understand, please continue to ask questions. Try to be specific when asking questions, though, so that we can do our best in answering your questions.

Sorry if I'm too verbose. Anyone with more experience, please feel free to chime in if I'm incorrect here.
thanks that makes alot, i understand it now. :)
Ok so make the changes to the code, added a high score still tring to find a way to save it.
For an example of reading and writing to a file, try visiting this link:

QB Express: Issue #4

For the saving of your high score table, take a look under the heading CREATING DATA FILES.

For loading your high score table when the program first starts up, try looking under the heading WORKING WITH SEQUENTIAL FILES.

See if those links help you. If not, please give us some specifics on what is giving you difficulties.
dim highscore as string
open "test.txt" for input as
DO WHILE NOT EOF
loop
LINE INPUT highscore
print highscore
sleep

my compiler gives tells me that is needs a expresion,
FileNumber = FreeFile
the website Doriath posted gave that before open test.txt for input filenumber
what do i need to fix it

i know that there is probally problems futher in the code, but 1 step at a time

This topic is closed to new replies.

Advertisement