Sign in to follow this  
eXtremeMachine

what is wrong

Recommended Posts

let a=0 let x=0 Print "game" Print "Press any key to continue" WHILE INKEY$ = "" X=RND() * 100 wend cls 1: input "What is the number"; a If x = a then print "yes, your number is"; a end if if x < a then print "Too high, your number is"; a goto 1 end if if x > a then print "Too high, your number is"; a goto 1 end if sleep theres the code, it is done in freebasic when i run it, it allways says "Too high, your number is and gives the number you put in" WHILE INKEY$ = "" X=RND() * 100 wend print x; sleep when i run that code it comes displays a different number each time why is the test command not working properly

Share this post


Link to post
Share on other sites
Goto makes baby jesus cry, kills your karma faster then anything else I know of, and it considered one of the worst things you can do. Goto littered code is one of the most unreadable things known to man (things intentionally made unreadable not taken into account), right up there with RSA encrypted documents and compressed video data. What you want to use are loops, not goto.

Share this post


Link to post
Share on other sites
Quote:
Original post by SirLuthor
Goto makes baby jesus cry, kills your karma faster then anything else I know of, and it considered one of the worst things you can do. Goto littered code is one of the most unreadable things known to man (things intentionally made unreadable not taken into account), right up there with RSA encrypted documents and compressed video data. What you want to use are loops, not goto.

There is nothing wrong with goto, especially in BASIC, and especially in very small apps like this one. Loop constructs in BASIC aren't too spectacular, and the code is perfectly readable as it is.

The real problem is this:
if x < a then
print "Too high, your number is"; a
goto 1
end if
if x > a then
print "Too high, your number is"; a
goto 1
end if
You have "Too high, your number is" in both if statements!

Share this post


Link to post
Share on other sites
Does it work correctly if you guess the number exactly?

One reason it might always be saying "Too high" is that you put the same message for the test cases of x < a and x > a. Try changing the print under x > a to "Too low".

Also, I think it would be easier (at least with the way you phrased your output) for people to read if you tested a = x, a < x, a > x since a is the variable that the user's guess is stored in.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ra
There is nothing wrong with goto, especially in BASIC, and especially in very small apps like this one.


*nods*

While it's not a concept to base a large program around, they are often the easiest way to do something. And oftentimes in basic the only way to do it with any amount of elegance.

There is nothing wrong with learning BASIC. I wrote space invaders, arkanoid, pac man, and a 3D wireframe renderer in BASIC. The simple syntax gave me the start in programming that I needed. I still believe it to be one of the best learning languages around.

Share this post


Link to post
Share on other sites
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=0
let x=0
let test = 1;
Print "game"
Print "Press any key to continue"
WHILE INKEY$ = ""
X=RND() * 100
wend
cls
DO
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 if
LOOP UNTIL test = 0
sleep


Notice, I switched your if statments to if, elseif, else statements. It's slightly quicker way and a bit more readable.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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() * 100
end 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() * 100
end function


and replace it with:


function NewNumber ()
NewNumber = RND() * 1000
end 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() * 1000
end sub


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


WHILE INKEY$ = ""
NewNumber
wend



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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this