confusedguy Posted April 24, 2007 Report Share Posted April 24, 2007 (edited) 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 SINGLEDIM higer AS STRINGDIM lower AS STRINGRANDOMIZE TIMERINPUT "Please enter in your guess: ", guesssecretnum = INT(1000 * RND + 1)IF guess > secretnum THENPRINT "Lower"IF guess < secretnum THENPRINT "Higher"IF guess = secretnum THENPRINT "You got it!"END IFEND IFEND IFi 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.CLSDIM secretnum AS SINGLEDIM guess AS SINGLEDIM higer AS STRINGDIM lower AS STRINGRANDOMIZE TIMERsecretnum = INT(1000 * RND + 1)WHILE secretnum <> guessINPUT "Please enter in your guess: ", guessIF guess > secretnum THENPRINT "Lower"IF guess < secretnum THENPRINT "Higher"IF guess = secretnum THENPRINT "You got it!"END IFEND IFEND IFwendonce again, any help is VERY appriciated. Edited April 24, 2007 by confusedguy Quote Link to post Share on other sites
bobbynichols Posted April 24, 2007 Report Share Posted April 24, 2007 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 SINGLEDIM higer AS STRINGDIM lower AS STRINGRANDOMIZE TIMERINPUT "Please enter in your guess: ", guesssecretnum = INT(1000 * RND + 1)IF guess > secretnum THENPRINT "Lower"IF guess < secretnum THENPRINT "Higher"IF guess = secretnum THENPRINT "You got it!"END IFEND IFEND IFi 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.CLSDIM secretnum AS SINGLEDIM guess AS SINGLEDIM higer AS STRINGDIM lower AS STRINGRANDOMIZE TIMERsecretnum = INT(1000 * RND + 1)WHILE secretnum <> guessINPUT "Please enter in your guess: ", guessIF guess > secretnum THENPRINT "Lower"IF guess < secretnum THENPRINT "Higher"IF guess = secretnum THENPRINT "You got it!"END IFEND IFEND IFwendonce again, any help is VERY appriciated.... What's a higer ? Quote Link to post Share on other sites
confusedguy Posted April 24, 2007 Author Report Share Posted April 24, 2007 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 SINGLEDIM higer AS STRINGDIM lower AS STRINGRANDOMIZE TIMERINPUT "Please enter in your guess: ", guesssecretnum = INT(1000 * RND + 1)IF guess > secretnum THENPRINT "Lower"IF guess < secretnum THENPRINT "Higher"IF guess = secretnum THENPRINT "You got it!"END IFEND IFEND IFi 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.CLSDIM secretnum AS SINGLEDIM guess AS SINGLEDIM higer AS STRINGDIM lower AS STRINGRANDOMIZE TIMERsecretnum = INT(1000 * RND + 1)WHILE secretnum <> guessINPUT "Please enter in your guess: ", guessIF guess > secretnum THENPRINT "Lower"IF guess < secretnum THENPRINT "Higher"IF guess = secretnum THENPRINT "You got it!"END IFEND IFEND IFwendonce again, any help is VERY appriciated.... What's a higer ?its simply a typo. its ment to say higher Quote Link to post Share on other sites
shanenin Posted April 25, 2007 Report Share Posted April 25, 2007 (edited) 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 neededDIM higer AS STRINGDIM lower AS STRING Edited April 25, 2007 by shanenin Quote Link to post Share on other sites
Matt Posted April 25, 2007 Report Share Posted April 25, 2007 Moving to the Programming Forum.... Quote Link to post Share on other sites
jcl Posted April 25, 2007 Report Share Posted April 25, 2007 (edited) CLSDIM guess AS SINGLERANDOMIZE TIMERsecretnum = INT(1000 * RND + 1)DOINPUT "Please enter your guess ", guessIF guess > secretnum THENPRINT "Lower"END IFIF guess < secretnum THENPRINT "Higher"END IFIF guess = secretnum THENPRINT "You got it!"EXIT DOEND IFLOOPHmm....Classic Basic:10 DIM guess AS SINGLE20 RANDOMIZE TIMER30 secretnum = INT(1000 * RND + 1)40 INPUT "Please enter your guess ", guess50 IF guess = secretnum THEN GOTO 9060 IF guess > secretnum THEN PRINT "Lower"70 IF guess < secretnum THEN PRINT "Higher"80 GOTO 4090 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 SubEnd ModuleObservation: 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. Edited April 25, 2007 by jcl Quote Link to post Share on other sites
bobbynichols Posted April 25, 2007 Report Share Posted April 25, 2007 (edited) Very nice. I finally found a freeware QBasic proggy at http://www.winsite.com/bin/Info?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." endIt's interesting to see the differences between the various types of BASIC that have evolved. Edited April 25, 2007 by bobbynichols Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.