-2

I am a beginner and been trying this for a while. Basically a program generates a random number and user has to guess this number and he has 5 chances. There must be a function which only checks if user has entered the number before or not. If yes user enters again until it is different from numbers entered before. Below is what i have currently written..

 Function dup(ByVal n As Integer, ByVal lop As Integer)
    Dim temp(5), kill As Integer
    If lop = 1 Then
        temp(1) = n
    ElseIf lop > 2 Then
        For count = 1 To lop - 1
            If n = temp(count) Then
                kill = 1
                count = lop - 1
            Else
                kill = 0
            End If
        Next
    End If
    Return kill

End Function
Sub Main()
    Dim x As New Random
    Dim num, guess, ans As Integer

    num = x.Next(0, 10)
    Console.WriteLine("NUMBER : " & num)
    For count = 1 To 5
        Console.WriteLine("ENTER YOUR GUESS")
        guess = Console.ReadLine
        ans = dup(guess, count)
        If ans = 0 Then
            If guess = num Then
                Console.WriteLine("CONGRATULATIONS")
            Else
                Console.WriteLine("TRY AGAIN")
            End If
        ElseIf ans = 1 Then
            Console.WriteLine("U HAVE USED THIS NUMBER. ENTER AGAIN")
            count = count - 1
        End If
    Next

    Console.ReadKey()

End Sub
Borislav Ivanov
  • 4,684
  • 3
  • 31
  • 55
  • Please clarify what is your exact question – GordonShumway Dec 25 '19 at 19:30
  • generate a random number. User will input his guess. if he has entered the number before, let user enter and user has 5 chances to guess number. if he guesses the number right, there should be a output saying "CONGRATULATIONS" – Muhammad Shamil Umar Dec 25 '19 at 20:08
  • You've got some serious misunderstanding of scope and lifetime on your variables. "Dim temp(5)" is cleared to zero every time you call "dup". Your loop "for count=1 to " is a different count (different scope and lifetime) that the one declared in Main, but it looks like you are trying to reduce the guess count if they have duplicated it. – Robert Richter Dec 25 '19 at 21:02

2 Answers2

0

I'm not understanding why you aren't testing the case with two guesses (but your "dup" function is broken anyway). The only temp index ever assigned is temp(1), but you are looking at indexes up to lop-1. However, even index #1 will get erased anyway every call to dup (lifetime issue).

This is more or less how I would do it.

private Guesses as new generic.list(of integer)

Function dup(ByVal n As Integer) as integer' (Please turn option strict on as you begin learning VB.NET)
   if Guesses.Contains(n) then return 1
   Guesses.Add(n)
   return 0
End Function

Sub Main()
    Dim x As New Random
    Dim num, guess, ans As Integer

    num = x.Next(0, 10)
    Console.WriteLine("NUMBER : " & num)
    Guesses.clear
' Personal preference, but I never use a for next loop if I'm changing the index or not updating it
   Count=1
   while Count<=5
    ' For count = 1 To 5 ' My preference is to use a while loop in this situation
        Console.WriteLine("ENTER YOUR GUESS")
        if not Integer.tryParse(Console.ReadLine, Guess) then ' Modified code to demonstrate validation
           Console.WriteLine("Please enter a numeric value")
        else 
           ans = dup(guess, count)
           If ans = 0 Then
               If guess = num Then
                   Console.WriteLine("CONGRATULATIONS")
               Else
                   Console.WriteLine("TRY AGAIN")
                   count+=1
               End If
           ElseIf ans = 1 Then
               Console.WriteLine("U HAVE USED THIS NUMBER. ENTER AGAIN")

           End If
       End If
    end while

    Console.ReadKey()

End Sub

Edit: Added validation code for the guess

Robert Richter
  • 278
  • 2
  • 9
0

It appears that your method of storing previous guesses is not working as intended.

What you can do is make an array once to store the values in. (It would be simpler using a List(Of Integer), but I will show using an array.) Then you can have a function which checks if the guessed number is in that array - the function should return a boolean value of True or False as appropriate. It could use 0 or 1, but True and False are understood to represent Yes and No respectively - a function returning zero might mean that there were zero problems.

Next, the Console.ReadLine() function returns a string, but what we want is a number. Luckily there are functions which will convert a string representation of a number into a number, in this case I used Integer.Parse.

When you have a number for something, say the maximum number of guesses, it is a good idea to assign it to a variable with a meaningful name. That way, it is easy to read what the code is intended to do and if you ever needed to change its value then you only need to do it in one place, which makes your life easier.

Note that array indexes start at zero.

Option Strict On

Module Module1

    Dim x As New Random()

    Function GuessedBefore(currentGuesses() As Integer, newGuess As Integer) As Boolean
        For i = 0 To currentGuesses.Length - 1
            If currentGuesses(i) = newGuess Then
                Return True
            End If
        Next

        Return False

    End Function

    Sub Main()
        Dim guess As Integer
        Dim num As Integer
        Dim maximumGuesses As Integer = 5
        Dim alreadyGuessed(maximumGuesses - 1) As Integer

        ' Set the values to something which won't be used.
        For i = 0 To alreadyGuessed.Length - 1
            alreadyGuessed(i) = -1
        Next

        num = x.Next(0, 10)

        Console.WriteLine("NUMBER: " & num)

        For count = 1 To maximumGuesses
            Console.Write("ENTER YOUR GUESS: ")
            guess = Integer.Parse(Console.ReadLine())

            If GuessedBefore(alreadyGuessed, guess) Then
                Console.WriteLine("YOU HAVE USED THIS NUMBER. ENTER AGAIN.")
                count = count - 1

            Else
                If guess = num Then
                    Console.WriteLine("CONGRATULATIONS!")
                    Exit For
                Else
                    alreadyGuessed(count - 1) = guess
                    Console.WriteLine("TRY AGAIN.")
                End If

            End If
        Next

        Console.ReadKey()

    End Sub

End Module

Notice the Exit For in the code where the user has guessed the number correctly: it exits the nearest For loop.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84