This a small addition to shamstar's fine comments
Rather than hardcoding the value in i.e. ['h','e','l','','o']
try using POS. POS will search a string for the instance of a substring... which is exactly what you are doing
var
GuessedChar : Char;
TheWord : String;
if pos(GuessedChar,TheWord) > 0 then
begin
// add your correct guess code in here
end
else
begin
// add your wrong guess code in here
end
Main Topics
Browse All Topics





by: shamstarPosted on 2003-10-30 at 14:05:00ID: 9653782
Heres something to get you started:
To write things out to the user via the console you use:
Writeln('the message goes here');
To read in input from the user use:
Readln(variable);
Hopefully you can easily use the Writeln (stands for write line) function.
To use the Readln function, you need to give it a variable for it to store the users input. This variable can
either be a string or a char type. When you want the user to provide you with a word, then use a string type.
When you want another user to take a guess at a letter, use a char type.
The final thing to do would be to check to see if the guessed character was in the word the first user chose.
To do this you can use something like:
var
GuessedChar : Char;
TheWord : String;
if GussedChar in ['h', 'e', 'l', 'l', 'o'] then
begin
// add your correct guess code in here
end
else
begin
// add your wrong guess code in here
end;
If you notice above, we are checking to see if the guessed character is in a set of other specific characters. For things to work correctly you will need the character from the chosen word in that set. Your best bet to do that is by accessing every character in a loop that runs for the number of characters in the word. This loop will access the characters using an array notation such as TheWord[1] to give you the first character from TheWord. To find the length of the word, use the Length funtion.
You may want to consider looking up the lenght function and sets in the delphi help.