Wheel of forutune App, Stuck!
I have to create a Wheel of Fortune type App using string manipulations, NOT arrays!. Steps:
-The user inputs a string (word) through a text box which is stored into a variable
-A new variable holds the length of the string with "********".
-So if the word is "bob", the label box displays "***"
-Next, They now guess a letter by pressing a command button
-An input box comes up, they guess a single letter which is stored into a variable
This is where i get stuck
-I have to search the string for the character they have guessed (Ex. the word is "bob", they guess an "o")
-If the character is indeed found within the string, the label box which is showing "***", has to change to "*o*" because they have guessed the "o"
How do i right this code out to replace each asterics (*) with the corresponding character?
[868 byte] By [
tygolfer] at [2007-11-11 9:59:10]

# 1 Re: Wheel of forutune App, Stuck!
This function may help u :
InStr(1, "bob", "o")
it returns the place of the "o" for example ; here it's "2" so u will need to change the "*" number "2" ;
this function may help u :
Replace(label.caption, "*", "o", 2, 1)
But the result will be the right side only so you need some thing like that :
label.caption = Left(label.caption, 1) & Replace(label.caption, "*", "o", 2, 1)
you can construct all this in one step :
label.caption = Left(label.caption, InStr(1, "bob", "o") - 1) & Replace(label.caption, "*", "o", InStr(1, "bob", "o"), 1)
but better to get it like that :
a = InStr(1, "bob", "o")
label.caption = Left(label.caption, a - 1) & Replace(label.caption, "*", "o", a, 1)
Amahdy at 2007-11-11 17:23:27 >

# 2 Re: Wheel of forutune App, Stuck!
ok thanx alot..im on my laptop soi dont have VB right now, but tomorrow ill try this code and get back to you. It sounds perfect!
ttyl.
# 3 Re: Wheel of forutune App, Stuck!
i have some code to present..one sec.
# 4 Re: Wheel of forutune App, Stuck!
This is what i got. For some reason im gettting a compiler error:Sub or function is not defined on the Replace function
Private Sub cmdGuess_Click()
Dim strEnter As String
Dim strNew As String
Dim strChar As String
Dim intPos As Integer
Dim a As String
strEnter = txtEnter.Text
strNew = String(Len(strEnter), "*")
lblDisplay.Caption = strNew
Do
strChar = InputBox("Guess a letter:", "Letter Entry")
If Len(strChar) > 1 Then
MsgBox "You can only enter one letter at a time!"
End If
Loop While Len(strChar) > 1
For intPos = 1 To Len(strEnter)
a = InStr(1, strEnter, strChar)
lblDisplay.Caption = Left(lblDisplay.Caption, a - 1) & Replace(lblDisplay.Caption, "*", strChar, a, 1)
Next
End Sub
# 5 Re: Wheel of forutune App, Stuck!
Check again this function is well defined , you can also search in the MSDN for it ;
search in the Index for : "Replace Function" you will find it ..
your code 'work' at my machine , but it has a logic error :
u must not put a "for loop" ;
For intPos = 1 To Len(strEnter) 'Make invalid output
a = InStr(1, strEnter, strChar)
lblDisplay.Caption = Left(lblDisplay.Caption, a - 1) & Replace(lblDisplay.Caption, "*", strChar, a, 1)
Next 'remove this too .
Amahdy at 2007-11-11 17:27:26 >

# 6 Re: Wheel of forutune App, Stuck!
Also u must check if "InStr(1, strEnter, strChar)" return 0 which mean the letter doesn't exists .. you must not continue in this case .
Amahdy at 2007-11-11 17:28:36 >

# 7 Re: Wheel of forutune App, Stuck!
try this function.
the string 'original' is the word to guess (like "bob")
'withStar' is the string to be displayed with the asterisks (like "***")
'toGuess' is the input letter (like "b")
The function substitutes the string 'toGuess' in the string 'withStar', in the right position, and returns the number os substitutions.
I used 'byref' and 'byval' to make clear that only one string is modified within the function
Have fun!
Private Function Change(ByVal original As String, ByRef withStar As String, ByVal toGuess As String) As Long
Dim k As Long
Dim count As Long
For k = 1 To Len(original)
If Mid$(original, k, 1) = toGuess Then
Mid$(withStar, k, 1) = toGuess
count = count + 1
End If
Next
Change = count
End Function
mstraf at 2007-11-11 17:29:29 >

# 8 Re: Wheel of forutune App, Stuck!
Private Function Change(ByVal original As String, ByRef withStar As String, ByVal toGuess As String) As Long
Dim k As Long
Dim count As Long
For k = 1 To Len(original)
If Mid$(original, k, 1) = toGuess Then
Mid$(withStar, k, 1) = toGuess
count = count + 1
End If
Next
Change = count
End Function
[/QUOTE]
What would this be equal to?
# 9 Re: Wheel of forutune App, Stuck!
it returns to the function the number of character in the string ;
for example : "bob", if you pass 'o' , it returns 1 .. if you pass 'b' it returns 2 .. and so on .
Amahdy at 2007-11-11 17:31:31 >

# 10 Re: Wheel of forutune App, Stuck!
this code may help u :
Private Function Change(ByVal original As String, _
ByRef withStar As String, _
ByVal toGuess As String) As Long
Dim k As Long
For k = 1 To Len(original)
If Mid$(original, k, 1) = toGuess Then
Mid$(withStar, k, 1) = toGuess
Change = Change + 1
End If
Next
End Function
------------------------
Private Sub Command1_Click()
Dim a As String
a = "***"
MsgBox Change("bob", a, "b")
MsgBox a
End Sub
Amahdy at 2007-11-11 17:32:32 >

# 11 Re: Wheel of forutune App, Stuck!
the output needs to be displayed in a label box *lblDisplay.Caption
So how would that line be coded...for example if i guess "o" in "bob". then the label box would show "*o*"
# 12 Re: Wheel of forutune App, Stuck!
ok I put "MsgBox" to demonstrate this ;
change both of them to :
lblDisplay.Caption = String(Len(strEnter), "*")
Change strEnter , lblDisplay.Caption , strChar
those two lines with "mstraf" function will do all the work ,,, does this help ?
Amahdy at 2007-11-11 17:34:39 >

# 13 Re: Wheel of forutune App, Stuck!
those two lines with "mstraf" function will do all the work ,,, does this help ?
i dont know what the mstraf function is, i have never learned it before.
Code:
Private Function Change(ByVal strEnter As String, _
ByRef strNew As String, _
ByVal strChar As String) As Long
Dim k As Long
For k = 1 To Len(strEnter)
If Mid(strEnter, k, 1) = strChar Then
Mid(strNew, k, 1) = strChar
Change = Change + 1
End If
Next
End Function
Private Sub cmdGuess_Click()
Dim strEnter As String
Dim strNew As String
Dim strChar As String
Dim intPos As Integer
Dim a As String
strEnter = txtEnter.Text
strNew = String(Len(strEnter), "*")
lblDisplay.Caption = strNew
Do
strChar = InputBox("Guess a letter:", "Letter Entry")
If Len(strChar) > 1 Then
MsgBox "You can only enter one letter at a time!"
End If
Loop While Len(strChar) > 1
lblDisplay.Caption = String(Len(strEnter), "*")
Change strEnter, lblDisplay.Caption, strChar
End of code
# 14 Re: Wheel of forutune App, Stuck!
I'm sorry u can't return by reference to an object "label" so the code must be :
dim a as string
a = String(Len(strEnter), "*")
Change strEnter , a, strChar
lblDisplay.Caption = a
Amahdy at 2007-11-11 17:36:37 >

# 15 Re: Wheel of forutune App, Stuck!
what u r using now is my new edited version of "mstraf" function anyway after all it must work now , isn't it ?
Amahdy at 2007-11-11 17:37:41 >

# 16 Re: Wheel of forutune App, Stuck!
hey great that works great.
Phase One complete :)
-now,when i want to guess a second letter (assuming i just guessed "o" in "bob"), the label redraws it self to "***" whe guess it pressed. So do i have to do a loop inside a loop? What do you think the code should be for that...i think there are a few ways.
# 17 Re: Wheel of forutune App, Stuck!
make the call every time by an abject not a variable ;
I mean take the format of asterisks from a label for example and put them in "a"; pass "a" to the function and the result label will contain the value of "a"; next time take the value of "a" from the result label which will aontain for example "*o*" not "***" .
Amahdy at 2007-11-11 17:39:41 >
