Qbasicnews.com
November 21, 2019, 05:23:42 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Simple question - "Duplicate definition"  (Read 6531 times)
HippyVanMan
New Member

Posts: 3


« on: February 23, 2007, 07:46:17 PM »

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
Logged
Plasma
Na_th_an
*****
Posts: 1770


WWW
« Reply #1 on: February 23, 2007, 08:46:15 PM »

QB thinks "s" is a sub from the statement "s -1". If you want to subtract 1 from s, use "s = s - 1".
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #2 on: February 23, 2007, 08:47:00 PM »

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
Logged
Skyler
Ancient Guru
****
Posts: 564



« Reply #3 on: February 23, 2007, 10:34:57 PM »

Wow, Moneo! You got in right on top of Plasma!
Logged

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
Anonymous
Guest
« Reply #4 on: February 24, 2007, 07:54:42 AM »

*shakes head in disbelief*
Logged
HippyVanMan
New Member

Posts: 3


« Reply #5 on: February 24, 2007, 10:19:50 AM »

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
Logged
Skyler
Ancient Guru
****
Posts: 564



« Reply #6 on: February 24, 2007, 12:46:22 PM »

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.
Logged

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
HippyVanMan
New Member

Posts: 3


« Reply #7 on: February 25, 2007, 06:58:43 AM »

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 Cheesy

It all works now, are there any ways to make it better, like cutting out useless statements or shortening it?
Logged
Skyler
Ancient Guru
****
Posts: 564



« Reply #8 on: February 25, 2007, 10:00:27 AM »

Check for zero lives before asking for another number. Also, indentatoin makes code MUCH easier to read.
Logged

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
dadsherm
Member
*
Posts: 75


« Reply #9 on: May 15, 2007, 02:13:03 PM »

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.
Logged

adsherm
Mac
Senior Member
**
Posts: 243


WWW
« Reply #10 on: May 16, 2007, 07:53:16 AM »

Quote from: "HippyVanMan"
Code:

number = INT(RND * 21)


It all works now, are there any ways to make it better, like cutting out useless statements or shortening it?


Well, that instruction can generate the number zero, which you don't allow the player to guess. Seems unfair to me LOL.

Anyway, below is a version that contains no GOTO's.

Also note the use of LCASE$ to allow the user to enter Y as well as y.

Mac

Code:

RANDOMIZE TIMER
Number = 1 + INT(RND * 20)
CLS
FOR Lives = 5 TO 1 STEP -1
  PRINT : PRINT "You have"; Lives; "lives."
  Guess = 0
  DO
    INPUT "Enter a number and press return: ", Try
    SELECT CASE Try
    CASE IS < 0:
      PRINT "You may not guess a number less than 0. Please enter another."
    CASE 0:
      PRINT "You may not guess 0. Please guess again."
    CASE IS > 20:
      PRINT "You may not guess a number greater than 20. Please enter another."
    CASE ELSE:
      Guess = Try
    END SELECT
  LOOP WHILE Guess = 0
  IF Guess = Number THEN EXIT FOR
  IF Guess < Number THEN
    PRINT "Your guess is smaller than the number."
  ELSE
    PRINT "Your guess is larger than the number."
  END IF
NEXT Lives
PRINT
IF Guess = Number THEN
  PRINT "Correct, you have won!!!"
ELSE
  PRINT "You have run out of lives."
  PRINT "The number was"; Number
END IF
DO
  PRINT
  LINE INPUT "Play again? y = yes , n = no: ", replay$
  replay$ = LCASE$(replay$)
  IF replay$ = "y" THEN RUN
LOOP WHILE replay$ <> "n"
CLS : SYSTEM
Logged
dadsherm
Member
*
Posts: 75


« Reply #11 on: May 16, 2007, 12:13:54 PM »

I just thought of a couple of other minor details....
Rather than having :

INPUT "Enter a number and press return"; guess
You may want to consider the following changes.

Give the player the boundries they may choose within.
(i;e " Enter a number between 1 and 20 and press return" )
Without giving it all away, you may want to use a different style of "INPUT" .
Why? Enter something other than a number and see what happens.  Shocked

Also, using a filter will greatly reduce the amount of code needed.
Granted, it won't give the player so many different things to enjoy reading!  Cheesy

To GOTO or not to GOTO, that is the question!
Everyone has their own preferences when it comes to programming.
I personally prefer to use the GOTO command for different reasons, one of which is because it acts (works) the same as JMP (JUMP)  :bounce:   , in assembly langauge.
Logged

adsherm
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!