Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 how can i errortrap a string
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
goroyoshi




PostPosted: 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 : array 1 .. 3 of int
var input : array 1 .. 3 of string
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)
            exit when strintok (input (1))
            put "please enter an integer"
        end loop
        guess1 := strint (input (1))
        if guess1 < x (1)
                then
            put "too low"
        end if
        if guess1 > x (1)
                then
            put "too high"
        end if
        exit when guess1 = x (1)
    end loop
    put "hooray you got it right"
end if
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)
            exit when strintok (input (2))
            put "please enter an integer"
        end loop
        guess2 := strint (input (2))
        if guess2 < x (2)
                then
            put "too low"
        end if
        if guess2 > x (2)
                then
            put "too high"
        end if
        exit when guess2 = x (2)
    end loop
    put "hooray you got it right"
end if
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)
            exit when strintok (input (3))
            put "please enter an integer"
        end loop
        guess3 := strint (input (3))
        if guess3 < x (3)
                then
            put "too low"
        end if
        if guess3 > x (3)
                then
            put "too high"
        end if
        exit when guess3 = x (3)
    end loop
    put "hooray you got it right"
end if



Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
goroyoshi




PostPosted: 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




PostPosted: 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
    end if
end loop



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
    end if
end loop



3. Without procedures (II):
Turing:


loop
    get difficulty
    exit when difficulty="easy" or difficulty="medium" or difficulty="difficult"
    %Re-ask for input
end loop
if difficulty = "easy" then
        %your code for the easy level
     
elsif difficulty = "medium" then
       %your code for the medium level
       
else
       %your code for the difficult level
end if
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: