# GuessNumber.vb loop URGENT help!

#### iStar19

##### Member
I need help with this simple guessnumber visual basic program. the book says to write the loops under the comments "Validate Input", but im not sure what to write, for I'm horrible at programming. This is the last program of the chapter and it's due today! Please help!

' GuessNumber.vb - This program allows a user to guess a number between 1 and 10.
' Input: User guesses numbers until they get it right
' Output: Tells users if they are right or wrong

Module GuessNumber
Sub Main()
Dim number As Integer ' Number to be guessed
Dim userNumber As Integer ' User's guess
Dim stringNumber As String ' String version of user's guess
Dim keepGoing As String ' Contains a "Y" or "N" determining if the user wants to continue

Randomize()
number = Int((10 * Rnd) + 1) ' Generate random number

' Prime the loop
keepGoing = InputBox\$("Do you want to guess a number? Enter Y or N")

' Validate input

' Enter loop if they want to play
Do While keepGoing = "Y"
' Get user's guess
stringNumber = InputBox\$("I'm thinking of a number. . Try to guess by entering a number between 1 and 10")
userNumber = Val(stringNumber)
' Validate input

' Test to see if the user guessed correctly
If userNumber = number Then
keepGoing = "N"
System.Console.WriteLine("You are a genius. That's correct!")
Else
keepGoing = InputBox\$("That's not correct. Do you want to guess again? Enter Y or N")
' Validate input
keepgoing = "Y"

End If
Loop ' End of Do While loop
End Sub ' End of Main() procedure
End Module ' End of GuessNumber Module

Last edited:

#### JuggaloBrotha

##### VB.NET Forum Moderator
Staff member
I'm going to head out on a limb here, is this homework?

#### iStar19

##### Member
well duh lol... this stuff is hard though. i just don't understand it at all.

#### JuggaloBrotha

##### VB.NET Forum Moderator
Staff member
Yea, see this approach to getting help isn't going to get you very far as we don't do people's homework for them and offering a bribe (payment) here only gets us admins/mods attention here for a thread closing.

What we do allow is helping with specific things your homework has, but even then we dont do it for you, you'd need to still provide your code (which you've done here) and we'll help with it, but again you're the one who needs to learn this stuff and you're the one who wont get far if you don't...

#### iStar19

##### Member
Yea, see this approach to getting help isn't going to get you very far as we don't do people's homework for them and offering a bribe (payment) here only gets us admins/mods attention here for a thread closing.

What we do allow is helping with specific things your homework has, but even then we dont do it for you, you'd need to still provide your code (which you've done here) and we'll help with it, but again you're the one who needs to learn this stuff and you're the one who wont get far if you don't...

Well actually, I will NEVER need this in my life. I am a Management major so I have absolutely no need to learn this. All majors within the College of Business at my university are required to take this pointless class. So in point, I WILL get very far without knowing how to do this thank you very much. You can close this thread after you have finished reading this because I don't know how.

#### JuggaloBrotha

##### VB.NET Forum Moderator
Staff member
Well actually, I will NEVER need this in my life. I am a Management major so I have absolutely no need to learn this. All majors within the College of Business at my university are required to take this pointless class. So in point, I WILL get very far without knowing how to do this thank you very much. You can close this thread after you have finished reading this because I don't know how.
I'm sure at a later time you'll understand why a lot of management curriculum's include 1 or more programming classes and I see you're gone back and changed your post(s) so I have no reason to close the thread. Now as for your issue of validating input I see at the bottom you're asking the user if they want to continue or not. I presume if they don't it's supposed to exit the loop. I see you've got the basics down, but I also see that if they don't guess the correct number they're forced to stay in the loop because you assign the KeepGoing variable "Y" which will ignore what they provided:
VB.NET:
``````If userNumber = number Then
keepGoing = "N"
System.Console.WriteLine("You are a genius. That's correct!")
Else
keepGoing = InputBox\$("That's not correct. Do you want to guess again? Enter Y or N")
' Validate input
keepgoing = "Y"

End If``````

#### iStar19

##### Member
Yes that was already included in the file, and it creates an infinite loop. I have taken out keepGoing = "Y", but now I can put in the same number like 30 times in a row and its never the right number. I'm just not seeing what I'm supposed to add, as the book gives me very simplified versions of this program. IT doesn't really show me how to complete this problem. Its just like "do it."

#### JuggaloBrotha

##### VB.NET Forum Moderator
Staff member
I dont get any infinite loops with this code:
VB.NET:
``````Public Sub Main()
Dim number As Integer ' Number to be guessed
Dim userNumber As Integer ' User's guess
Dim stringNumber As String ' String version of user's guess
Dim keepGoing As String ' Contains a "Y" or "N" determining if the user wants to continue

Randomize()
number = CInt((10 * Rnd()) + 1) ' Generate random number

' Prime the loop
keepGoing = InputBox\$("Do you want to guess a number? Enter Y or N")

' Validate input

' Enter loop if they want to play
Do While keepGoing = "Y"
' Get user's guess
stringNumber = InputBox("I'm thinking of a number. . Try to guess by entering a number between 1 and 10")
userNumber = CInt(stringNumber)
' Validate input

' Test to see if the user guessed correctly
If userNumber = number Then
keepGoing = "N"
System.Console.WriteLine("You are a genius. That's correct!")
Else
keepGoing = InputBox("That's not correct. Do you want to guess again? Enter Y or N")
' Validate input
End If
Loop ' End of Do While loop
End Sub ' End of Main() procedure``````
Eventually I am able to guess the correct number

#### iStar19

##### Member
Thanks so much man. The book said nothing about modifying the existing code though. It just said to add to it, and all you did was delete an entry and change Int to CInt, which it did not teach me about. Also, the entire time was thinking that the program generated a random number each time I guessed the wrong answer, and thanks to your help, I was able to run the program while changing or adding nothing. All I had to do is delete keepGoing = "Y"

#### JuggaloBrotha

##### VB.NET Forum Moderator
Staff member
No problem, I take it you've met your deadline? Does programming still seem frustrating?

#### iStar19

##### Member
No actually I haven't. This was the fifth chapter out of six that was required for a 100%. I just really can't wrap my head around this stuff anymore. I was beast at it at first, then I stopped for over a month, so when I looked in the book again last week I was totally lost in the terminology and everything. I was a Computer Information Systems major, but then I changed my major shortly after attending the programming class. It's just so boring to me. I only have to make a 58% on my final today to get a B for the semester... GPA dropper, but oh well.

Replies
8
Views
974
Replies
1
Views
720
Replies
1
Views
992
Replies
4
Views
748
Replies
2
Views
3K