ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I have a variable set as;
Word_used=bakar
The variable was then changed to a variable called $hidden “-----“ using the tr command.
My program accepts a single character entered by the user, checks it to see if exists in the word_used variable.
If the character exists in the word_used variable then the program should input that letter into the hidden word. For example if the letter k was entered by the user, the program should print --k--
i cannot get the program to enter the letter into the word if is correct.
The following is my code:
while [ $lives -ge 1 ]; do
counter1=1
echo
echo $hiddenword
echo "please enter a letter "
read input_letter # accepts the input
clear
if [ -z $input_letter ]; then # checks if nothing has been entered
echo "you have not entered anything"
echo "please enter a letter"
validation3=0 # resets loop
else
input_letter_length=`expr length $input_letter` # get length of input letter
if [ $input_letter_length -eq 1 ]; then
while [ $counter1 -le $word_length ];do # checks the letter input against the word_used
character=`echo $word_used | cut -b$counter1` # check each one at a time
if [ $input_letter = $character ];then
# THIS IS WHERE THE PROGRAM SHOULD ENTER THE LETTER INPUT BY THE USER INTO THE VARIABLE $HIDDENWORD. I NEED HELP ON HOW TO DO THIS.
fi
counter1=`expr $counter1 + 1` #continues loop until counter = length of word
done
# prints out the results for each letter
echo $hidden_word # prints out the hidden word with the correct letters
echo "you have entered the following letters $list_of_answers"
lives=`expr $lives - 1`
number_of_tries=`expr length $list_of_answers`
echo "you have had $number_of_tries tries"
echo "you have another $lives lives"
validation3=1 # this loop will contiue until lives has run out or incorrect data is entered
fi
fi
In order to facilitate commentary, I have added line #'s to your code & put it in "Code" block below:
Code:
1: while [ $lives -ge 1 ]; do
2: counter1=1
3: echo
4: echo $hiddenword
5: echo "please enter a letter "
6: read input_letter
7: # accepts the input
8: clear
9:
10: # checks if nothing has been entered
11: if [ -z $input_letter ]; then
12: echo "you have not entered anything"
13: echo "please enter a letter"
14: validation3=0
15: # resets loop
16: else
17: input_letter_length=`expr length $input_letter`
18: # get length of input letter
19: if [ $input_letter_length -eq 1 ]; then
20:
21: # checks the letter input against the word_used
22: while [ $counter1 -le $word_length ]; do
23: character=`echo $word_used | cut -b$counter1`
24: # check each one at a time
25: if [ $input_letter = $character ]; then
26:
27: # THIS IS WHERE THE PROGRAM SHOULD ENTER THE LETTER
28: # INPUT BY THE USER INTO THE VARIABLE $HIDDENWORD.
29: # I NEED HELP ON HOW TO DO THIS.
30:
31: fi
32: counter1=`expr $counter1 + 1`
33: #continues loop until counter = length of word
34: done
35: # prints out the results for each letter
36: echo $hidden_word
37: # prints out the hidden word with the correct letters
38: echo "you have entered the following letters " \
"$list_of_answers"
39: lives=`expr $lives - 1`
40: number_of_tries=`expr length $list_of_answers`
41: echo "you have had $number_of_tries tries"
42: echo "you have another $lives lives"
43: validation3=1
44: # this loop will contiue until lives has run out
45: # or incorrect data is entered
46: fi
47: fi
48: done
In order to minimize horizontal scrolling in either of my browsers (Konqueror 3.3.2 & Opera 9); I put your original comments on the following line, changed the indents to 3 spaces, & broke l. 38 w/ a '\'.
I have a variable set as;
Word_used=bakar
The variable was then changed to a variable called $hidden “-----“ using the tr command.
My program accepts a single character entered by the user, checks it to see if exists in the word_used variable.
If the character exists in the word_used variable then the program should input that letter into the hidden word. For example if the letter k was entered by the user, the program should print --k--
i cannot get the program to enter the letter into the word if is correct.
The following is my code:
while [ $lives -ge 1 ]; do
You haven't initialized $lives.
Quote:
counter1=1
echo
echo $hiddenword
echo "please enter a letter "
read input_letter # accepts the input
clear
if [ -z $input_letter ]; then # checks if nothing has been entered
echo "you have not entered anything"
echo "please enter a letter"
validation3=0 # resets loop
else
input_letter_length=`expr length $input_letter` # get length of input letter
POSIX shells (bash, ksh, ash, etc.) have a parameter expansion to get the length of a variable:
Code:
input_letter_length=${#input_letter}
Quote:
if [ $input_letter_length -eq 1 ]; then
while [ $counter1 -le $word_length ];do # checks the letter input against the word_used
character=`echo $word_used | cut -b$counter1` # check each one at a time
if [ $input_letter = $character ];then
# THIS IS WHERE THE PROGRAM SHOULD ENTER THE LETTER INPUT BY THE USER INTO THE VARIABLE $HIDDENWORD. I NEED HELP ON HOW TO DO THIS.
fi
counter1=`expr $counter1 + 1` #continues loop until counter = length of word
done
You don't need a counter; use temporary variables, extracting and removing the first character each time through the loop:
Code:
word_right=$used_word
hidden_right=$hidden
hidden=
while :
do
[ -z "$word_right" ] && break
right_temp=${word_right#?}
word_letter=${word_right%"$right_temp"}
hidden_temp=${hidden_right#?}
hidden_letter=${hidden_right%"$hidden_temp"}
if [ "$word_letter" = "$input_letter" ]
then
hidden_letter=$input_letter
fi
hidden=$hidden$hidden_letter
word_right=$right_temp
hidden_right=$hidden_temp
done
Quote:
# prints out the results for each letter
echo $hidden_word # prints out the hidden word with the correct letters
echo "you have entered the following letters $list_of_answers"
lives=`expr $lives - 1`
In a POSIX shell, you do not need expr; the shell can do arithmetic:
Code:
lives=$(( $lives - 1 ))
Quote:
number_of_tries=`expr length $list_of_answers`
echo "you have had $number_of_tries tries"
echo "you have another $lives lives"
validation3=1 # this loop will contiue until lives has run out or incorrect data is entered
fi
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.