validating user input

username

New member
Joined
Jan 25, 2010
Messages
3
Programming Experience
Beginner
Okay, first of all, hi everyone.

This may be a simple problem, but it's caused me no end of trouble...
What I have is a (soon-to-be) hangman game. Now, in hangman, you enter a word and someone else has to guess it.
In my program, the idea is that a word is entered, and someone else tries to guess it. The problem is that I don't want the person putting in the word to use numbers or symbols, but spaces are allowed.

I imagine there would be a fairly simple solution to this, but I've tried to figure it out and have failed, and I haven't found anything online so far.

If anyone could help me, I would be very grateful.

Thanks :)

p.s. A second problem I have, which is no smaller than the first, is dealing with uppercase and lowercase letters. If the inputter adds capitals at the beginnings of words, this could cause a problem for the guesser.

Thanks again.
 
The simple way is to just loop through the string and test each character. Char.IsLetter will tell you whether a character is a letter or not and you can compare to the space character directly. String implements IEnumerable, so you can treat it as though it's an array:
VB.NET:
For Each ch In myString
    If Not Char.IsLetter(ch) AndAlso ch <> " "c Then
If you want to get a bit more advanced then you can use a regular expression. I'm really no expert there though. If you want more info then look up the Regex class and you can find regex patterns on the web.

With regards to the last question, the String.IndexOf method will give you the index of a character in a string and it's overloaded, allowing you to perform a case-insensitive comparison.
 
Well, I don't know what to say, save "thank you".

Speedy response, accurate, and not complicated!



edit: A problem which I now seem to have is splitting the word into an array of its constituent letters.

This did seem to work before:


********Guess = console.readline()
********NewPhrase = unknown word (the idea is to guess it)


********Dim LettersInWord() as string = NewPhrase.Split(New Char() {""})



********Dim i As Integer

********If Len(Guess) = 1 Then
************For i = 0 To Len(NewPhrase)
****************If Guess = LettersInWord(i) Then
********************Console.WriteLine("Correct.")
****************Else : Console.WriteLine("Incorrect.")
****************End If
************Next
********Else : (unimportant, and not affecting this section of code)
********End If

So, what I want is for the input word to be broken down into an array, and then to test each letter in turn against the one that the person entered as a guess. if it is the same then the message "correct" comes up, otherwise, "incorrect".

For now the desired output is a list, of length equal to the length of the NewPhrase, looking something like this:

eg. newphrase = hello
guess = l

output:
Incorrect
Incorrect
Correct
Correct
Incorrect


Currently I get the following error message:
"A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll"
then four of these:
"A first chance exception of type 'System.IndexOutOfRangeException' occurred in MyVersionHangman.exe"



If anyone can tell me what the program doesn't like about this, that would be great. Frankly, I'm not seeing a problem (hence I'm here, I suppose), but it's not working so... yeah.

Anyway, thanks in advance.
 
Last edited:
Should use the mid command!
Guess = console.readline
word = whatever
Dim Letters(len(word)
dim x as integer

for x = 1 to len(word)
Letters(x) = mid(word,1,x)
next

That will put each letter into an array

to check it it would be like
do
console.writeline("Enter a letter!")
dim letter as string
dim correct as boolean = true
letter = console.readline
if len(letter) > 1 then
console.writeline("Incorrect input!")
correct = false
end if
loop until correct = true
for x = 1 to len(word)
if mid(word,1,x) = letter
then
console.writeline("Correct!")
end if

something like that!!
 
for x = 1 to len(word)
Letters(x) = mid(word,1,x)
next

That will put each letter into an array
Pointless, see code example in post 2. String also has array functionality for accessing a specific char using the default Chars property, but that is not needed here.
 
Back
Top