|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#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
|
|
|
|
|
|
#2 |
|
Professional gadfly
|
Use the StrReverse function if it is available:
Code:
lstNames.AddItem StrReverse(strNames(lngIndex)) |
|
|
|
|
|
#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
|
|
|
|
|
|
#4 | |
|
Member (5 bit)
Join Date: Feb 2005
Posts: 18
|
Quote:
|
|
|
|
|
|
|
#5 |
|
Professional gadfly
|
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
|
|
|
|
|
|
#6 | |
|
Member (5 bit)
Join Date: Feb 2005
Posts: 18
|
Quote:
|
|
|
|
|
|
|
#7 | |
|
Professional gadfly
|
Quote:
|
|
|
|
|
|
|
#8 | |
|
Member (5 bit)
Join Date: Feb 2005
Posts: 18
|
Quote:
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
#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. |
|
|
|
|
|
#11 | |
|
Member (5 bit)
Join Date: Feb 2005
Posts: 18
|
Quote:
Code:
Dim strLetters(1 To 26) As Boolean
strLetters = False
If strLetters() = True Then
MsgBox(strMessage)
|
|
|
|
|
|
|
#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 |
|
|
|
|
|
#13 | |
|
Member (5 bit)
Join Date: Feb 2005
Posts: 18
|
Quote:
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
|
|
|
|
|
|
|
#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 |
|
|
|
|
|
#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.
|
|
|
|
|
|
#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. Last edited by foolishone; 03-11-2005 at 10:04 PM. |
|
|
|
|
|
#17 |
|
Soopa Squishy
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 |
|
|
|
|
|
#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 |
|
|
|
|
|
#19 |
|
Soopa Squishy
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) |
|
|
|
|
|
#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)? |
|
|
|
|
|
#21 |
|
Soopa Squishy
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. |
|
|
|
|
|
#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
|
|
|
|
|
|
#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 |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|