Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple question - "Duplicate definition"
#1
This is the code for a simple guessing game, not all variable or labels are that helpfully names but it's so simple it's not hard to understand.
Code:
start: INPUT "Enter a number"; guess
number = INT(RND * 20 + 1)
s = 5
SLEEP
CLS
yyy: IF (guess > 20 OR guess < 0) THEN
PRINT "Your guess is more than 20 or less than zero. Enter another."
GOTO start
ELSEIF (guess > number) THEN
PRINT "You guessed larger than the number"
s -1
GOTO yyy
ELSEIF (guess < number) THEN
PRINT "You guessed a smaller number"
s -1
GOTO yyy
END IF

When I attempt to interpret this program the QBasic interpreter 1.1 highlights the first declaration (or possibly it's called an initialisation) of s, saying "Duplicate definition" which I do not understand, as I haven't defined s anywhere else, unless the statements lower down in which I attempt to decrement s have the incorrect syntax.

Any help would be appreciated and I expect you'll all know the answer very quickly Tongue
Reply
#2
QB thinks "s" is a sub from the statement "s -1". If you want to subtract 1 from s, use "s = s - 1".
Reply
#3
I didn't test it, but just looking at your code, where you have
s -1
you probably mean
s = s-1

Try it.

Regards..... Moneo
Reply
#4
Wow, Moneo! You got in right on top of Plasma!
In the beginning, there is darkness – the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
Reply
#5
*shakes head in disbelief*
Reply
#6
As cha0s was likely commenting on, it obviously will not work as it is not finished. But that error is gone, so thank you.

As you'll also realise, it won't really work that well right now either. But it seems to work well enough to be tested, and I can't seem to get the right number, even though my logic, or picking the midpoint each time, is flawless. Have I misused the integer function?

Code:
start: INPUT "Enter a number"; guess
number = INT(RND * 20)
s = 5
SLEEP
CLS
yyy: IF (guess > 20 OR guess < 0) THEN
PRINT "Your guess is more than 20 or less than zero. Enter another."
SLEEP
GOTO start
CLS
ELSEIF s = 0 THEN
PRINT "YOU LOSE!"
SLEEP
END
ELSEIF (guess > number) THEN
PRINT "You guessed larger than the number"
s = s - 1
SLEEP
CLS
GOTO start
ELSEIF (guess < number) THEN
PRINT "YOu guessed a smaller number"
s = s - 1
SLEEP
CLS
GOTO start
ELSEIF (guess = number) THEN
SLEEP

END IF
Reply
#7
First of all, every time you guess, it calculate a new random number. Put this line:
Code:
number = INT(RND * 20)
before the start: tag.
Secondly, you should use a RANDOMIZE TIMER statement at the beginning of the program. Otherwise it will generate the same series of "random" numbers each time.
Third, I'd use SELECT CASE instead of all those ifs and elseifs- like this:
Code:
SELECT CASE guess
        CASE IS > 20
                PRINT "Your guess is more than 20. Try again."
                GOTO start
        CASE IS < 0
                PRINT "Your guess is less than 0. Try again."
                GOTO start
        CASE IS > number
                PRINT "Your guess is larger than the number."
                s = s - 1
                GOTO start
        CASE IS < number
                PRINT "Your guess is smaller than the number."
                s = s - 1
                GOTO start
        CASE number
                PRINT "YOU WIN!"
                END
END SELECT

I'd tell you exactly how to make it work, but it's better to teach you to fish than feed you.
EDIT: By the way, I think cha0s was referring to my post, not yours. I don't recall anyone(Well, except maybe Z!re) acting like that over someone else's code.
In the beginning, there is darkness – the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
Reply
#8
Code:
beggining:
RANDOMIZE TIMER
number = INT(RND * 21)

lives = 5

start:
CLS
PRINT "You have"; lives; "lives."
PRINT
INPUT "Enter a number and press return"; guess

IF lives = 0 THEN
PRINT "You have run out of lives."
END
END IF

SELECT CASE guess

CASE IS = 0
PRINT "You may not guess 0. Please guess again."
SLEEP
CLS
GOTO start

CASE IS > 20
PRINT "You may not guess a number greater than 20. Please enter another."
SLEEP
CLS
GOTO start

CASE IS < 0
PRINT "You may not guess a number less than 0. Please enter another."
SLEEP
CLS
GOTO start

CASE IS > number
PRINT "Your guess is larger than the number."
lives = lives - 1
SLEEP
CLS
GOTO start

CASE IS < number
PRINT "Your guess is smaller than the number."
lives = lives - 1
SLEEP
CLS
GOTO start

CASE number
PRINT "Correct, you have won!!!"
GOTO replay


END SELECT

replay: INPUT "Play again? y = yes , n = no"; replay$

IF replay$ = "y" THEN
GOTO beggining

END IF

END

Thankyou Big Grin

It all works now, are there any ways to make it better, like cutting out useless statements or shortening it?
Reply
#9
Check for zero lives before asking for another number. Also, indentatoin makes code MUCH easier to read.
In the beginning, there is darkness – the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
Reply
#10
Since the player doesn't lose any "lives" if "0" or a negative number are used, you may want to consider changing your code:

CASE IS = 0
PRINT "You may not guess 0. Please guess again."
SLEEP
CLS
GOTO start

TO
CASE IS < 1
PRINT " This game is for the LIVING only!
PRINT "Please enter a number between 1 and 20"
SLEEP
CLS
GOTO START

This will eliminate the need for the "< 0" CASE.
adsherm
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)