Posted: Thu Mar 03, 2011 8:08 pm Post subject: how can i errortrap a string
What is it you are trying to achieve?
I would like to error trap a string
What is the problem you are having?
I HAVE NO IDEA HOW TO DO IT!
Describe what you have tried to solve this problem
nothing (please read above comment)
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing:
var x :array1.. 3ofint var input :array1.. 3ofstring var difficulty :string put"what difficulty would you like to do" put"easy is a number from 1 - 10" put"medium is a number from 1 - 100" put"hard is a number from 1 - 1000" get difficulty % this is the part that i want to errortrap if difficulty ="easy" then put"guess a number from 1 to 10" var guess1 :int randint(x (1),1, 10) loop loop get input (1) exitwhenstrintok(input (1)) put"please enter an integer" endloop
guess1 :=strint(input (1)) if guess1 < x (1) then put"too low" endif if guess1 > x (1) then put"too high" endif exitwhen guess1 = x (1) endloop put"hooray you got it right" endif
if difficulty ="medium" then put"guess a number from 1 to 100" var guess2 :int randint(x (2),1, 100) loop loop get input (2) exitwhenstrintok(input (2)) put"please enter an integer" endloop
guess2 :=strint(input (2)) if guess2 < x (2) then put"too low" endif if guess2 > x (2) then put"too high" endif exitwhen guess2 = x (2) endloop put"hooray you got it right" endif
if difficulty ="hard" then put"guess a number from 1 to 1000" var guess3 :int randint(x (3),1, 1000) loop loop get input (3) exitwhenstrintok(input (3)) put"please enter an integer" endloop
guess3 :=strint(input (3)) if guess3 < x (3) then put"too low" endif if guess3 > x (3) then put"too high" endif exitwhen guess3 = x (3) endloop put"hooray you got it right" endif
Please specify what version of Turing you are using
4.1.1
Sponsor Sponsor
Tony
Posted: Thu Mar 03, 2011 8:21 pm Post subject: Re: how can i errortrap a string
goroyoshi @ Thu Mar 03, 2011 8:08 pm wrote:
I HAVE NO IDEA HOW TO DO IT!
You can start by describing what error trapping a string means.
Posted: Thu Mar 03, 2011 8:23 pm Post subject: Re: how can i errortrap a string
alright, i already have an errortrap for my integers (when you put text/decimal it will say please enter an integer)
so i want it that when you enter for the string "difficulty" that if its wrong or is a number, it will say please enter one of the three options
clear?
ihsh
Posted: Thu Mar 03, 2011 11:34 pm Post subject: Re: how can i errortrap a string
First, I think you should let difficulty be an int variable. That way you don't have to worry about the person typing the word in capital letters.
Also, I don't think arrays are necessary here because you are only going to use the variables to store one value each. Furthermore, your code is a bit long. The only difference between the three difficulty levels is the range of the random number, yet you are having three almost-identical sections of code under each condition. You could just make a variable for upper-limit of the random number and save yourself some space.
Anyway, suppose that you are just adding the errortrap without changing any other part of the code there are many ways that you can choose, for example:
1. Use procedures
Turing:
procedure easyGame
%your code for the easy level end easyGame
procedure mediumGame
%your code for the medium level end mediumGame
procedure difficultGame
%your code for the difficult level end difficultGame
loop get difficulty
if difficulty ="easy"then
easyGame
exit elsif difficulty ="medium"then
mediumGame
exit elsif difficulty ="difficult"then exit
difficultGame
exit else%If choice is invalid, %Re-ask for input endif endloop
2. Without procedures:
Turing:
loop get difficulty
if difficulty ="easy"then %your code for the easy level exit elsif difficulty ="medium"then %your code for the medium level exit elsif difficulty ="difficult"then %your code for the difficult level exit
else%If choice is invalid, %Re-ask for input endif endloop
3. Without procedures (II):
Turing:
loop get difficulty
exitwhen difficulty="easy"or difficulty="medium"or difficulty="difficult" %Re-ask for input endloop if difficulty ="easy"then %your code for the easy level
elsif difficulty ="medium"then %your code for the medium level