Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-01-2005, 10:28 AM   #1
Member (5 bit)
 
Join Date: Feb 2005
Posts: 18
Visual Basic: Reverse Display Names

This program lets a user input 5 names in an inputbox then displays it in the listbox, but the problem is I want the user who entered the name revealed backwards in the outcome. Example when a user enters Peter as the name, the outcome in the listbox would be reteP. Heres the code for displaying names but I don't know what to put for reverse coding.

Code:
Option Explicit

Private Sub cmdDone_Click()
  Unload Me
End Sub

Private Sub Form_Load()
  Randomize
  Dim strNames(1 To 5) As String
  Dim lngIndex As Long
 
  'Load names
  For lngIndex = LBound(strNames) To UBound(strNames)
      strNames(lngIndex) = InputBox("Enter student's first name:", "Students")
  Next lngIndex
 
  'Display names
  For lngIndex = LBound(strNames) To UBound(strNames)
      lstNames.AddItem strNames(lngIndex)
  Next lngIndex

End Sub
ecchikinetic is offline   Reply With Quote
Old 03-01-2005, 10:32 AM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
Use the StrReverse function if it is available:

Code:
lstNames.AddItem StrReverse(strNames(lngIndex))
doctorgonzo is offline   Reply With Quote
Old 03-01-2005, 10:46 AM   #3
Member (5 bit)
 
Join Date: Feb 2005
Posts: 18
I don't wanna add another thread for this but heres my next problem.

Basically this program when executed it generates 10 random number between 1-99 Then when the max button is clicked, it displays a message saying that the max number out of 10 random number is x. Same goes for min command.

Code:
Option Explicit

Private Sub cmdDone_Click()
    Unload Me
End Sub

Private Sub cmdMax_Click()

lblMessage.Caption = "Max Number =" & 

End Sub

Private Sub cmdMin_Click()

lblMessage.Caption = "Min Number =" & 

End Sub

Private Sub Form_Load()
    Randomize
    Dim intNumbers As Integer
    Dim intRandomNumbers As Integer

For intNumbers = 1 To 10
    intRandomNumbers = Int((99 * Rnd) + 1)
    lstOutcome.AddItem intRandomNumbers
Next

End Sub

Private Sub lstOutcome_Click()
    lstOutcome.Clear
End Sub
ecchikinetic is offline   Reply With Quote
Old 03-01-2005, 10:57 AM   #4
Member (5 bit)
 
Join Date: Feb 2005
Posts: 18
Quote:
Originally Posted by doctorgonzo
Use the StrReverse function if it is available:

Code:
lstNames.AddItem StrReverse(strNames(lngIndex))
By the way, the reverse name display works now thanks.
ecchikinetic is offline   Reply With Quote
Old 03-01-2005, 11:08 AM   #5
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
For the min and max functions, you will have to do something like this (here is the pseudo-code for max):

Code:
Dim x as integer
x=0
For each item in lstOutcome
    If item.value>x then x=item.value
Next
max=x
Min would be similar.
doctorgonzo is offline   Reply With Quote
Old 03-01-2005, 01:08 PM   #6
Member (5 bit)
 
Join Date: Feb 2005
Posts: 18
Quote:
Originally Posted by doctorgonzo
For the min and max functions, you will have to do something like this (here is the pseudo-code for max):

Code:
Dim x as integer
x=0
For each item in lstOutcome
    If item.value>x then x=item.value
Next
max=x
Min would be similar.
I don't understand, what's item defined as?
ecchikinetic is offline   Reply With Quote
Old 03-01-2005, 01:46 PM   #7
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
Quote:
Originally Posted by ecchikinetic
I don't understand, what's item defined as?
That's not literal code, that's just p-code. The general idea is that you have to loop through every item in your listbox and check to see if that is the highest value so far. You can loop through them however you want.
doctorgonzo is offline   Reply With Quote
Old 03-01-2005, 02:31 PM   #8
Member (5 bit)
 
Join Date: Feb 2005
Posts: 18
Quote:
Originally Posted by doctorgonzo
That's not literal code, that's just p-code. The general idea is that you have to loop through every item in your listbox and check to see if that is the highest value so far. You can loop through them however you want.
I'm not use to p-code. It's better if literal code is displayed instead.
ecchikinetic is offline   Reply With Quote
Old 03-01-2005, 03:06 PM   #9
Member (5 bit)
 
Join Date: Feb 2005
Posts: 18
I'm trying to make hangman game from VB, it went fine but I need help with keeping track of the letters that the user has inputted in the inputbox. If the user has entered the same letter twice then a messagebox appears saying that the user has already guessed that letter. Heres the code:

Code:
Option Explicit

Private Sub cmdPlayGame_Click()
    Const strSentinel As String = "!"

    Dim strSecretWord As String, intSecretWordLength As Integer
    Dim intNumberOfGuesses As Integer
    Dim strGuess As String, strWordGuessedSoFar As String
    Dim intLetterPos As Integer

    strSecretWord = "magic"
    intSecretWordLength = Len(strSecretWord)
    strWordGuessedSoFar = String(intSecretWordLength, "-")
    lblWord.Caption = strWordGuessedSoFar

    intNumberOfGuesses = 0

    strGuess = InputBox("Guess a letter (! to guess word)", "Hangman")
    Do While strGuess <> strSentinel
        intNumberOfGuesses = intNumberOfGuesses + 1
        For intLetterPos = 1 To intSecretWordLength
            If StrComp(strGuess, Mid(strSecretWord, intLetterPos, 1), vbTextCompare) = 0 Then
                Mid(strWordGuessedSoFar, intLetterPos, 1) = strGuess
            End If
        Next intLetterPos
        lblWord.Caption = strWordGuessedSoFar
        strGuess = InputBox("Guess a letter (! to guess word)", "Hangman")
    Loop

    If strGuess = strSentinel Then
        strGuess = InputBox("Guess the word")
    End If

    If StrComp(strGuess, strSecretWord, vbTextCompare) = 0 Then
        MsgBox "You win! It took you " & intNumberOfGuesses & " guesses."
    Else
        MsgBox "You lose. Press OK to display secret word."
    End If

    lblWord.Caption = strSecretWord
End Sub

Private Sub cmdDone_Click()
    Unload Me
End Sub
ecchikinetic is offline   Reply With Quote
Old 03-04-2005, 03:19 AM   #10
Member (10 bit)
 
Join Date: Jul 2002
Location: University of California, Santa Barbara
Posts: 800
Can VB do arrays? It's been a long time since I've touched VB, but if it has arrays then simply make a 26 element array of booleans, initially all false, and as each letter is selected change it to true. Then each time the user enters a letter, check to see if the corresponding entry in the array is true or not.

Also,
If strGuess = strSentinel Then
strGuess = InputBox("Guess the word")
End If
is redundant. The only way for that code to execute is when the while loop has ended, which happens when strGuess=strSentinel.
mattg2k4 is offline   Reply With Quote
Old 03-04-2005, 01:37 PM   #11
Member (5 bit)
 
Join Date: Feb 2005
Posts: 18
Quote:
Originally Posted by mattg2k4
Can VB do arrays? It's been a long time since I've touched VB, but if it has arrays then simply make a 26 element array of booleans, initially all false, and as each letter is selected change it to true. Then each time the user enters a letter, check to see if the corresponding entry in the array is true or not.

Also,
If strGuess = strSentinel Then
strGuess = InputBox("Guess the word")
End If
is redundant. The only way for that code to execute is when the while loop has ended, which happens when strGuess=strSentinel.
Like this?

Code:
    Dim strLetters(1 To 26) As Boolean
    strLetters = False

    If strLetters() = True Then
    MsgBox(strMessage)
I dunno what else to put or what else am I missing here.
ecchikinetic is offline   Reply With Quote
Old 03-08-2005, 08:10 PM   #12
Member (9 bit)
 
Join Date: Nov 2004
Location: Flint, MI
Posts: 256
If you didn't get it already, I would do a for loop through the 26 letter, setting them all to false


Dim x as integer 'Used as a counting variable in loop

For x=1 to 26
strLetter(x)=False
Next x

Each time through the loop it will add one to x until it gets to 26, and it stops, loop complete, strLetter(1)...strLetter(26)=false

^fo
foolishone is offline   Reply With Quote
Old 03-10-2005, 12:25 PM   #13
Member (5 bit)
 
Join Date: Feb 2005
Posts: 18
Quote:
Originally Posted by foolishone
If you didn't get it already, I would do a for loop through the 26 letter, setting them all to false


Dim x as integer 'Used as a counting variable in loop

For x=1 to 26
strLetter(x)=False
Next x

Each time through the loop it will add one to x until it gets to 26, and it stops, loop complete, strLetter(1)...strLetter(26)=false

^fo
I tried it but it doesn't seem to be reponding to the letters I entered. When I entered the letter 's' twice, the message didn't appear saying that you used that letter already. Heres what I got so far.

Code:
Option Explicit

Private Sub cmdPlayGame_Click()
    Const strSentinel As String = "!"
    Dim x As Integer 'Used as a counting variable in loop
    Dim strLetters(1 To 26) As String

    Dim strSecretWord As String, intSecretWordLength As Integer
    Dim intNumberOfGuesses As Integer
    Dim strGuess As String, strWordGuessedSoFar As String
    Dim intLetterPos As Integer
    Dim strMessage As String
    
    strMessage = "You already have guessed this letter, Try again."
    strSecretWord = "magic"
    intSecretWordLength = Len(strSecretWord)
    strWordGuessedSoFar = String(intSecretWordLength, "-")
    lblWord.Caption = strWordGuessedSoFar

    intNumberOfGuesses = 0
    
    For x = 1 To 26
    strLetters(x) = False
        If strLetters(x) = True Then
            MsgBox (strMessage)
        End If
    Next x
    
    strGuess = InputBox("Guess a letter (! to guess word)", "Hangman")
    Do While strGuess <> strSentinel
        intNumberOfGuesses = intNumberOfGuesses + 1
        For intLetterPos = 1 To intSecretWordLength
            If StrComp(strGuess, Mid(strSecretWord, intLetterPos, 1), vbTextCompare) = 0 Then
                Mid(strWordGuessedSoFar, intLetterPos, 1) = strGuess
                End If
        Next intLetterPos
        lblWord.Caption = strWordGuessedSoFar
        strGuess = InputBox("Guess a letter (! to guess word)", "Hangman")
    Loop

    If strGuess = strSentinel Then
        strGuess = InputBox("Guess the word")
    End If

    If StrComp(strGuess, strSecretWord, vbTextCompare) = 0 Then
        MsgBox "You win! It took you " & intNumberOfGuesses & " guesses."
    Else
        MsgBox "You lose. Press OK to display secret word."
    End If

    lblWord.Caption = strSecretWord
End Sub

Private Sub cmdDone_Click()
    Unload Me
End Sub
ecchikinetic is offline   Reply With Quote
Old 03-10-2005, 10:07 PM   #14
Member (9 bit)
 
Join Date: Nov 2004
Location: Flint, MI
Posts: 256
In the for loop that I told you to use, you have an if statement, which is fine, but one of the hardest things about programming is the placement of code,that's your error.

For x = 1 To 26
strLetters(x) = False
If strLetters(x) = True Then
MsgBox (strMessage)
End If
Next x

The loop looks at strLetters(1) and sets it to false, second line. In the third line you contridict yourself and say if strLetters(1) = True... IT WILL NEVER BE TRUE because in the preceding you set it to false.

I don't know how you have your game set up, but do you have a keydown, keypress event or how do you get from the user which letter they pushed.

On the form activate, maybe something else, depending on how your game is set up, you might want to have a select case for every letter (somebody must know a better, quicker way). Have the letter they picked assign an index in the array strLetters.

EX.

Private Sub frmGame_????()

Select Case KeyCode 'KeyCode is a predefined variable in VB and will return the string that is associated with that particular character
Case vbKeyA
If strLetters(1)=false then
'Allow to enter it in
Else
Msgbox (strMessage)
End if

Case vbKeyB
.
.
.

Attached is the name for every character on the keyboard. It tells you what is sent to VB when a key is pressed.

Post back with comments, the same ideas and techiques are used all progamming languages, so get it the first time

Arrays are your friend.

^fo
Attached Files
File Type: txt VB Key Codes.txt (2.9 KB, 124 views)
foolishone is offline   Reply With Quote
Old 03-11-2005, 11:28 AM   #15
Member (5 bit)
 
Join Date: Feb 2005
Posts: 18
Maybe if I send the whole program it'd be better and see it for yourself.
Attached Files
File Type: zip Hangman.zip (1.8 KB, 122 views)
ecchikinetic is offline   Reply With Quote
Old 03-11-2005, 07:50 PM   #16
Member (9 bit)
 
Join Date: Nov 2004
Location: Flint, MI
Posts: 256
Its a bad file.

If you want, email me at foolishone at gmail.com

^fo

EDIT: I threw something together, give it a look over and post back with comments/questions, just adapt to your program and use. Instead of a huge select case/if statement, it just sends the letter as a parameter to a method. The "program" does work, try it. Copy the text into a blank code screen. The form itself is completely blank, no buttons, labels, etc.
Attached Files
File Type: txt hangman.txt (3.0 KB, 105 views)

Last edited by foolishone; 03-11-2005 at 10:04 PM.
foolishone is offline   Reply With Quote
Old 03-11-2005, 11:00 PM   #17
Soopa Squishy
 
shadowbreaker513's Avatar
 
Join Date: Sep 2004
Posts: 1,175
ecchikinetic not to go offtopic, but your code looks really messy. You may want to space it a bit better to make it easier to read.

Also, what does

Option Explicit

do? I only use

Option Strict
__________________
Intel Core 2 Duo E6420 / EVGA NF68-T1 680i SLI / EVGA 320mb 8800GTS / Western Digital Raptor 74GB / 4x1024 Mushkin eXtreme Performance / OCZ Modstream 520w / Antec P180
shadowbreaker513 is offline   Reply With Quote
Old 03-12-2005, 10:44 AM   #18
Member (9 bit)
 
Join Date: Nov 2004
Location: Flint, MI
Posts: 256
Never heard of Option Strict.

Option Explicit says that every varaiable has to be declared somewhere in the program. VB allows you to just use variable names and if it finds that it is undeclared will declare it itself during runtime. With Option Explicit it won't run, or at least not without an error message, unless every variable is declared.

^fo
foolishone is offline   Reply With Quote
Old 03-12-2005, 11:30 AM   #19
Soopa Squishy
 
shadowbreaker513's Avatar
 
Join Date: Sep 2004
Posts: 1,175
Option Strict makes it so you must cast a variable type change instead of letting it do it automatically. It can help prevent crashes, errors, and extra RAM uses in longer programs.
For example with option strict off

Dim number As Double
number= txtNumber.Text

is valid. With Option Strict On, that wouldn't even compile. You would have to have

Dim number As Double
number=CDbl(txtNumber.Text)
shadowbreaker513 is offline   Reply With Quote
Old 03-12-2005, 11:46 AM   #20
Member (9 bit)
 
Join Date: Nov 2004
Location: Flint, MI
Posts: 256
Whats with the number=CDbl(txtNumber.Text) ?

What is CDbl?

If I want to get a number from a text box I would use Num(txtNumber.text). Does yours protect against letters and numbers in the text box or something?

^fo

EDIT: Or would that be CreateDouble (CDbl)?
foolishone is offline   Reply With Quote
Old 03-12-2005, 01:53 PM   #21
Soopa Squishy
 
shadowbreaker513's Avatar
 
Join Date: Sep 2004
Posts: 1,175
When you just type in

number = txtNumber.Text

When number is a double, vb automatically changes the string to a double. This can created excess RAM usage and strange problems in general. Especially in longer programs.

number = CDbl(txtNumber.Text)

That tells vb to case the string in txtNumber as a double. This avoids the problems I listed above when it is not used. Option Strict On makes not casting an error and won't let the program compile. It's not commonly used as programs will stil run without it, just not as well as they should.

In case you were wondering, the cases are CDbl(), CInt(), CStr(), and CBool(). Boolean might be CBol(), but I haven't had to use that one too much so I'm not totally sure.
shadowbreaker513 is offline   Reply With Quote
Old 03-14-2005, 12:32 PM   #22
Member (5 bit)
 
Join Date: Feb 2005
Posts: 18
I adapted it to my codes but it says "supscript out of range" and it points to this line of code:

If letters(x) Then 'If in the array, letter([the number you sent it]), is true

Heres the code with yours combined:

Also what does the "Shift" do?

Code:
Option Explicit
Dim letters(1 To 26) As Boolean
Dim x As Integer

Private Sub cmdPlayGame_Click()
    Const strSentinel As String = "!"

    Dim strSecretWord As String, intSecretWordLength As Integer
    Dim intNumberOfGuesses As Integer
    Dim strGuess As String, strWordGuessedSoFar As String
    Dim intLetterPos As Integer
    Dim strMessage As String
    
    strMessage = "You already have guessed this letter, Try again."
    strSecretWord = "magic"
    intSecretWordLength = Len(strSecretWord)
    strWordGuessedSoFar = String(intSecretWordLength, "-")
    lblWord.Caption = strWordGuessedSoFar

    intNumberOfGuesses = 0
    
    strGuess = InputBox("Guess a letter (! to guess word)", "Hangman")
    Do While strGuess <> strSentinel
        intNumberOfGuesses = intNumberOfGuesses + 1
        Call checkLetter(x)
        For intLetterPos = 1 To intSecretWordLength
            If StrComp(strGuess, Mid(strSecretWord, intLetterPos, 1), vbTextCompare) = 0 Then
                Mid(strWordGuessedSoFar, intLetterPos, 1) = strGuess
                End If
        Next intLetterPos
        lblWord.Caption = strWordGuessedSoFar
        strGuess = InputBox("Guess a letter (! to guess word)", "Hangman")
    Loop
    
    If strGuess = strSentinel Then
        strGuess = InputBox("Guess the word")
    End If

    If StrComp(strGuess, strSecretWord, vbTextCompare) = 0 Then
        MsgBox "You win! It took you " & intNumberOfGuesses & " guesses."
    Else
        MsgBox "You lose. Press OK to display secret word."
    End If

    lblWord.Caption = strSecretWord
End Sub

Private Sub cmdDone_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    Dim KeyCode As Integer, x As Integer
    
    For x = 1 To 26
        letters(x) = False
    Next x
    
    Select Case KeyCode 'VB stores the string in this variable, KeyCode
    
        Case vbKeyA
            checkLetter (1)
            
        Case vbKeyB
            checkLetter (2)
        
        Case vbKeyC
            checkLetter (3)
            
        Case vbKeyD
            checkLetter (4)
        
        Case vbKeyE
            checkLetter (5)
        
        Case vbKeyF
            checkLetter (6)
        
        Case vbKeyG
            checkLetter (7)
            
        Case vbKeyH
            checkLetter (8)
        
        Case vbKeyI
            checkLetter (9)
        
        Case vbKeyJ
            checkLetter (10)
        
        Case vbKeyK
            checkLetter (11)
        
        Case vbKeyL
            checkLetter (12)
        
        Case vbKeyM
            checkLetter (13)
        
        Case vbKeyN
            checkLetter (14)
        
        Case vbKeyO
            checkLetter (15)
        
        Case vbKeyP
            checkLetter (16)
        
        Case vbKeyQ
            checkLetter (17)
        
        Case vbKeyR
            checkLetter (18)
        
        Case vbKeyS
            checkLetter (19)
        
        Case vbKeyT
            checkLetter (20)
        
        Case vbKeyU
            checkLetter (21)
        
        Case vbKeyV
            checkLetter (22)
        
        Case vbKeyW
            checkLetter (23)
        
        Case vbKeyX
            checkLetter (24)
        
        Case vbKeyY
            checkLetter (25)
        
        Case vbKeyZ
            checkLetter (26)
            
    End Select
End Sub

Sub checkLetter(x As Integer) 'letterNum is the variable that will be referred to that is holding the value you sent it above, 1,2,3...

Dim message As String

message = "Please choose a different letter"

If letters(x) Then       'If in the array, letter([the number you sent it]), is true
    MsgBox message              'Display message
Else
    letters(x) = True    'If this is the first time for letterNum, change it to true
End If

End Sub
ecchikinetic is offline   Reply With Quote
Old 03-14-2005, 08:40 PM   #23
Member (9 bit)
 
Join Date: Nov 2004
Location: Flint, MI
Posts: 256
I see that you have checkLetter(x) in the loop, but what are you sending it? VB defaults all variables to zero, so if you did not give x a value, it gets zero, which according to the array letter(x) doesn't exist because its 1 to 26.

The select case takes the key that you send it and converts it to a numerical array subscript/index/etc.

checkLetter checks to see the state of the array index/subscript. If it is true, it throws up the string (which you need to change in what I gave you) in a msgbox, and if it is false it changes it to true and allows you to guess it.

You are not sending the select case anything to convert to a number so the checkLetter can't to its job and check with the array. You are bypassing the conversion of letter to number. checkLetter(x) is sending the value of x to checkLetter, which is expecting a integer (1-26) and can't do it because you sent it a letter, or a variable, which happens to be zero because VB defaults it to zero. Give x a value, which it can send to checkLetter.


How are the users entering in a value?

If the form is active, and no other objects have been pushed, pressed, etc. the form has set focus. And when this is true and a letter has been pressed, it corresponds that key/letter to a number, which checkLetter will receive as a parameter and check the array for a use.

If you don't get what I am saying post back with comments/questions. It is a little hard to understand.

^fo
foolishone is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:18 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2