I have spent about 40 hours on this code so far. I have finally cleared the faults from it. I am a new coder. What I am trying to get it to do is to display the users name is the "type your guess " box as in "type your guess Robert." and the additional task is to provide feedback to the user that says if the number is too high or too low until they eventually guess it, providing feedback that they guessed it. Although no code errors come up when you run the code, the code crashes once you initially input a guess.

'Initialization Section

Option Explicit

Const cGreetingMsg = "Pick a number between 1 - 100"

Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses, strOkToEnd1


intRandomNo = FormatNumber(Int((100 * Rnd) + 1))

intNoGuesses = 0

function GetPlayersName()

GetPlayersName = InputBox("What is your first name? ")

'Main Processing Section

End Function

select case strOkToEnd

  case 1 

   If FormatNumber(intUserNumber) < intRandomNo Then 

    'Test to see if the user's guess was too low

    MsgBox "Your guess was too low. Try again", ,cGreetingMsg

    End If 

case 2

 If FormatNumber(intUserNumber) > intRandomNo Then

 MsgBox "Your guess was too high. Try again", , cGreetingMsg

 End If 

'Loop until either the user guesses correctly or the user clicks on Cancel

   End Select

'Generate a random number

Do Until strOkToEnd = "yes"

'Prompt user to pick a number

  intUserNumber = InputBox("Type your guess:"), GetPlayersName, cGreetingMsg

intNoGuesses = intNoGuesses + 1
  'See if the user provided an answer  

   If Len(intUserNumber) <> 0 Then

'Make sure that the player typed a number

    If IsNumeric(intUserNumber) = True Then


   If IsNumeric(intUserNumber) = False Then

   MsgBox "Sorry.  You did not enter a number.  Try Again."

  strOkToEnd = "yes"

  'Test to see if the user's guess was correct

    If FormatNumber(intUserNumber) = intRandomNo Then

       MsgBox "Congratulations! You guessed it. The number was " & _

       intUserNumber & "." & vbCrLf & vbCrLf & "You guessed it " & _

          "in " & intNoGuesses & " guesses.", ,cGreetingMsg

         strOkToEnd = "yes"

    End If

    End If

  End If

    End If

Exit Do 


Your code does not work. It doesn't just run incorrectly. It doesn't run at all. I think a rewrite is in order. Start with pseudo-code as in

welcome user (get name)
generate random number

done = false

    ask user for a number   
        case too low
            "too low - guess again"
        case too high
            "too high - guess again
        case correct
            done = true
    end select

until done

A few suggestions:

Process numbers as numbers instead of strings. Also (and this is opersonal preference) I believe that the mainline code should appear first, followed by functions and subroutines. Don't write a function that is longer than the code it is going to replace unless you are going to call it from several places.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.