0

I'm trying to make a slot machine program. This procedure that I'm trying to do will assign a name to 3 randomly generated numbers. For some reason I'm getting a conversion error saying that it cant convert the integer to a string. I tried cstr() as well but the problem persisted

 Sub GenerateNumbers()
    Dim numbers(2) As Integer
    Dim names(5) As String
    Dim x As Integer

    names(0) = "Cherries"
    names(1) = "Oranges"
    names(2) = "Plums"
    names(3) = "Bells"
    names(4) = "Melons"
    names(5) = "Bar"

    For x = 0 To 2
        numbers(x) = names(CInt(Int((6 * Rnd()) + 1)))
    Next x
End Sub

gives me error: conversion from string "Oranges" to type 'Integer' is not valid

2
  • 1
    Do not call Randomize() in succession, if called without an argument it re-initializes (seeds) random number generator with system time so if you call it repeatedly in a short while the values returned by Rnd() most likely won't be random. Call Randomize() without argument once before using Rnd() for the first time or, if you want to get same pseudo-random sequence on every run, with a constant argument. Commented Dec 9, 2011 at 20:26
  • Yes, I figured that out a little bit ago. My problem now is that I cant get Number1,2 or 3 to convert to the text value it is assigned. I tried numberx = cstr("text"). That and the OP of course. Commented Dec 9, 2011 at 20:26

2 Answers 2

1

The problem is that you are getting a random string from the names array and trying to assign it to numbers, which is declared as an array of integers. Of course this is not gonna work.

Apart from that, there is also the issue with out of bounds index as Eric pointed out.


Edit in response to comments:

To get the text values of those randomly generated slot machine results you just need to declare the array to store results as strings, same way as names is declared.

To be able to get the results from a separate procedure, you need to change it from Sub to Function, which is a procedure that can return a value, an array of strings in this case. Then you can call this function from your Main or any other procedure and store the returned value in a variable.

I also corrected the part with random result generation.

Module SlotMachine

    Sub Main()
        Dim slotResults As String()

        'Get the results
        slotResults = GenerateResults()

        'Some further processing of results here, e.g. print results to console
        For Each item In slotResults
            Console.WriteLine(item)
        Next

        'Wait for keypress before closing the console window 
        Console.ReadLine()
    End Sub

    'Generates random results
    Function GenerateResults() As String()
        Dim results(2) As String
        Dim names(5) As String
        Dim x As Integer

        names(0) = "Cherries"
        names(1) = "Oranges"
        names(2) = "Plums"
        names(3) = "Bells"
        names(4) = "Melons"
        names(5) = "Bar"

        Randomize()

        For x = 0 To 2
            results(x) = names(Int(6 * Rnd()))
        Next x

        Return results
    End Function

End Module
Sign up to request clarification or add additional context in comments.

7 Comments

@Kudla69 I do not understand completely what you want to achieve with the code. What should numbers array represent?
numbers represents the 3 different slot machine results. The outcome should come out as something like: Cherries Oranges Bar or Melons Melons Melons etc.
@Kudla69 And do you also need to store the number value of those random results or only the text value?
Thank you so much. How could I change it so that it will output the results? I do apologize, I'm still learning this stuff
@Kudla69 Added a simple for each loop, which will iterate through the results and print them to the console one by one. As you are learning this stuff I assume you are doing this inside a VB console application.
|
1

Int(6 * Rnd()) will get you 0-5, if you +1, then overflow

1 Comment

oops, alright I changed it to int(5 * Rnd()) + 1. But it's still giving me the same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.