BestTechie Forums: I Am Way Confused - BestTechie Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

I Am Way Confused QBasic question


#1 User is offline   confusedguy 

  • Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 24-April 07

Posted 24 April 2007 - 05:41 PM

Alright, so i have this assignment for a class, and i have what i hope to be most of it done, but i just cant seem to figure out how to do a certain part of it.

the assignment calls for making a program where a random number is generated and the user gets a chance to input a guess. the program will then print out if the guess is lower or higher or the same as the random number. and if it isent the same, it alows them to continue to guess untill they get it right.

so far i have been able to get the random number generated and for it to tell you if your guess is higher lower or the same. but i cant figure out how to get it to allow the user to continue to guess without re-starting the program and thus getting a compleatly different random number.


here is my code so far.

DIM guess AS SINGLE
DIM higer AS STRING
DIM lower AS STRING

RANDOMIZE TIMER
INPUT "Please enter in your guess: ", guess
secretnum = INT(1000 * RND + 1)
IF guess > secretnum THEN
PRINT "Lower"
IF guess < secretnum THEN
PRINT "Higher"
IF guess = secretnum THEN
PRINT "You got it!"
END IF
END IF
END IF



i am pretty sure i have to put in a DO WHILE or DO UNTIL loop. but whenever i try to put one in it gives me either an error saying that its a loop or do without the other. and if it dosent give me that error message, then it just allows me to enter my first guess and nothing else. dosent even print out if its higher lower or if i guess it correctly.


please, im not asking for anyone to do this for me, but i have been trying to figure this out for hours, and im about to burn out. i just need someone to either tell me how to make it loop, or at least send me in the right direction.

thanks.


alright, so i just got a loop to work with it, but now after i enter in the first value, it either just continues to let me enter in guesses without telling me if its higher or lower. or it keeps sayin lower until once again, it just stops telling me. why is that happening?

here is my updated code.


CLS
DIM secretnum AS SINGLE
DIM guess AS SINGLE
DIM higer AS STRING
DIM lower AS STRING

RANDOMIZE TIMER
secretnum = INT(1000 * RND + 1)
WHILE secretnum <> guess
INPUT "Please enter in your guess: ", guess
IF guess > secretnum THEN
PRINT "Lower"
IF guess < secretnum THEN
PRINT "Higher"
IF guess = secretnum THEN
PRINT "You got it!"
END IF
END IF
END IF
wend


once again, any help is VERY appriciated.

This post has been edited by confusedguy: 24 April 2007 - 05:42 PM


#2 User is offline   bobbynichols 

  • Howdy !
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1746
  • Joined: 08-September 05
  • Operating System:XP SP3

Posted 24 April 2007 - 06:32 PM

View Postconfusedguy, on Apr 24 2007, 03:41 PM, said:

Alright, so i have this assignment for a class, and i have what i hope to be most of it done, but i just cant seem to figure out how to do a certain part of it.

the assignment calls for making a program where a random number is generated and the user gets a chance to input a guess. the program will then print out if the guess is lower or higher or the same as the random number. and if it isent the same, it alows them to continue to guess untill they get it right.

so far i have been able to get the random number generated and for it to tell you if your guess is higher lower or the same. but i cant figure out how to get it to allow the user to continue to guess without re-starting the program and thus getting a compleatly different random number.


here is my code so far.

DIM guess AS SINGLE
DIM higer AS STRING
DIM lower AS STRING

RANDOMIZE TIMER
INPUT "Please enter in your guess: ", guess
secretnum = INT(1000 * RND + 1)
IF guess > secretnum THEN
PRINT "Lower"
IF guess < secretnum THEN
PRINT "Higher"
IF guess = secretnum THEN
PRINT "You got it!"
END IF
END IF
END IF



i am pretty sure i have to put in a DO WHILE or DO UNTIL loop. but whenever i try to put one in it gives me either an error saying that its a loop or do without the other. and if it dosent give me that error message, then it just allows me to enter my first guess and nothing else. dosent even print out if its higher lower or if i guess it correctly.


please, im not asking for anyone to do this for me, but i have been trying to figure this out for hours, and im about to burn out. i just need someone to either tell me how to make it loop, or at least send me in the right direction.

thanks.


alright, so i just got a loop to work with it, but now after i enter in the first value, it either just continues to let me enter in guesses without telling me if its higher or lower. or it keeps sayin lower until once again, it just stops telling me. why is that happening?

here is my updated code.


CLS
DIM secretnum AS SINGLE
DIM guess AS SINGLE
DIM higer AS STRING
DIM lower AS STRING

RANDOMIZE TIMER
secretnum = INT(1000 * RND + 1)
WHILE secretnum <> guess
INPUT "Please enter in your guess: ", guess
IF guess > secretnum THEN
PRINT "Lower"
IF guess < secretnum THEN
PRINT "Higher"
IF guess = secretnum THEN
PRINT "You got it!"
END IF
END IF
END IF
wend


once again, any help is VERY appriciated.


... What's a higer ?

#3 User is offline   confusedguy 

  • Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 24-April 07

Posted 24 April 2007 - 06:39 PM

View Postbobbynichols, on Apr 24 2007, 11:32 PM, said:

View Postconfusedguy, on Apr 24 2007, 03:41 PM, said:

Alright, so i have this assignment for a class, and i have what i hope to be most of it done, but i just cant seem to figure out how to do a certain part of it.

the assignment calls for making a program where a random number is generated and the user gets a chance to input a guess. the program will then print out if the guess is lower or higher or the same as the random number. and if it isent the same, it alows them to continue to guess untill they get it right.

so far i have been able to get the random number generated and for it to tell you if your guess is higher lower or the same. but i cant figure out how to get it to allow the user to continue to guess without re-starting the program and thus getting a compleatly different random number.


here is my code so far.

DIM guess AS SINGLE
DIM higer AS STRING
DIM lower AS STRING

RANDOMIZE TIMER
INPUT "Please enter in your guess: ", guess
secretnum = INT(1000 * RND + 1)
IF guess > secretnum THEN
PRINT "Lower"
IF guess < secretnum THEN
PRINT "Higher"
IF guess = secretnum THEN
PRINT "You got it!"
END IF
END IF
END IF



i am pretty sure i have to put in a DO WHILE or DO UNTIL loop. but whenever i try to put one in it gives me either an error saying that its a loop or do without the other. and if it dosent give me that error message, then it just allows me to enter my first guess and nothing else. dosent even print out if its higher lower or if i guess it correctly.


please, im not asking for anyone to do this for me, but i have been trying to figure this out for hours, and im about to burn out. i just need someone to either tell me how to make it loop, or at least send me in the right direction.

thanks.


alright, so i just got a loop to work with it, but now after i enter in the first value, it either just continues to let me enter in guesses without telling me if its higher or lower. or it keeps sayin lower until once again, it just stops telling me. why is that happening?

here is my updated code.


CLS
DIM secretnum AS SINGLE
DIM guess AS SINGLE
DIM higer AS STRING
DIM lower AS STRING

RANDOMIZE TIMER
secretnum = INT(1000 * RND + 1)
WHILE secretnum <> guess
INPUT "Please enter in your guess: ", guess
IF guess > secretnum THEN
PRINT "Lower"
IF guess < secretnum THEN
PRINT "Higher"
IF guess = secretnum THEN
PRINT "You got it!"
END IF
END IF
END IF
wend


once again, any help is VERY appriciated.


... What's a higer ?



its simply a typo. its ment to say higher

#4 User is offline   shanenin 

  • UberTechie
  • Group: Moderator
  • Posts: 3755
  • Joined: 24-February 05
  • Location:rochester , MN USA
  • Operating System:Ubuntu 8.04

Posted 24 April 2007 - 07:03 PM

I had some code, but after looking at it it does not work. I may give it a another shot.

I did notice something. You only need to use the DIM statement with variables

these two lines, I don't think are needed
DIM higer AS STRING
DIM lower AS STRING

This post has been edited by shanenin: 24 April 2007 - 08:31 PM


#5 User is offline   Matt 

  • root
  • Group: Contributor
  • Posts: 3512
  • Joined: 23-August 04
  • Location:Ohio
  • Operating System:Ubuntu 9.04 (64 bit), Vista Home Premium (32 bit)

Posted 24 April 2007 - 08:12 PM

Moving to the Programming Forum....

#6 User is offline   jcl 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1304
  • Joined: 30-August 04
  • Location:The Internet
  • Operating System:Arch

Posted 24 April 2007 - 11:15 PM

CLS

DIM guess AS SINGLE

RANDOMIZE TIMER
secretnum = INT(1000 * RND + 1)

DO
INPUT "Please enter your guess ", guess
IF guess > secretnum THEN
PRINT "Lower"
END IF
IF guess < secretnum THEN
PRINT "Higher"
END IF
IF guess = secretnum THEN
PRINT "You got it!"
EXIT DO
END IF
LOOP


Hmm....

Classic Basic:

10 DIM guess AS SINGLE
20 RANDOMIZE TIMER
30 secretnum = INT(1000 * RND + 1)
40 INPUT "Please enter your guess ", guess
50 IF guess = secretnum THEN GOTO 90
60 IF guess > secretnum THEN PRINT "Lower"
70 IF guess < secretnum THEN PRINT "Higher"
80 GOTO 40
90 PRINT "You got it!"


Visual Basic.NET:

Module Hello
  Sub Main()
	Dim rnd As New Random
	Dim secretNum As Integer = rnd.Next(1000)
	Dim guess As Integer
	Dim done As Boolean = False

	Do Until done
	  Console.Write("Please enter your guess ")
	  guess = Val(Console.ReadLine)
	  Console.WriteLine(guess)
	  If guess > secretNum Then
		Console.WriteLine("Lower")
	  ElseIf guess < secretNum Then
		Console.WriteLine("Higher")
	  Else
		Console.WriteLine("You got it!")
		done = True
	  End If
	Loop

  End Sub
End Module


Observation: the usability of a Basic dialect is inversely proportional to its sophistication. Unstructured (classic) Basic is more usable than structured Basic (QB), structured Basic is more usable than object-oriented Basic (VB.NET). The VB.NET program is twice the size of the unstructured program, took ten times as long to write, and looks like mush. The QB program is in between.

This post has been edited by jcl: 25 April 2007 - 03:21 AM


#7 User is offline   bobbynichols 

  • Howdy !
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1746
  • Joined: 08-September 05
  • Operating System:XP SP3

Posted 25 April 2007 - 03:06 AM

Very nice.

I finally found a freeware QBasic proggy at http://www.winsite.c...o?2000000035557 and jcl's first prog works fine.

I also have a freeware proggy called Just Basic v. 1.0 that I picked up somewhere that had this exercise as a tutorial program:

 ' Here is an interactive HI-LO
	' Program

[start]
	guessMe = int(rnd(1)*1000) + 1

	' Clear the screen and print the title and instructions
	cls
	print "HI-LO"

	print

	print "I have decided on a number between one"
	print "and a thousand, and I want you to guess"
	print "what it is.  I will tell you to guess"

	print "higher or lower, and we'll count up"
	print "the number of guesses you use."

	print

[ask]
	' Ask the user to guess the number and tally the guess
	input "OK.  What is your guess?"; guess

	' Now add one to the count variable to count the guesses
	let count = count + 1

	' check to see if the guess is right
	if guess = guessMe then goto [win]
	' check to see if the guess is too low
	if guess < guessMe then print "Guess higher."

	' check to see if the guess is too high
	if guess > guessMe then print "Guess lower."

	' go back and ask again
	goto [ask]

[win]
	' beep once and tell how many guesses it took to win
	beep
	print "You win!  It took "; count; " guesses."

	' reset the count variable to zero for the next game
	let count = 0

	' ask to play again
	input "Play again (Y/N)"; play$
	if instr("YESyes", play$) > 0 then goto [start]

	print "Press ALT-F4 to close this window."

	end


It's interesting to see the differences between the various types of BASIC that have evolved.

This post has been edited by bobbynichols: 25 April 2007 - 03:07 AM


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users