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

Username:   Password: 
 RegisterRegister   
 errortrap
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
nin




PostPosted: Thu Jan 18, 2007 8:31 pm   Post subject: errortrap

how do u errortrap words?
what i mean is that in my game , for the answerable questions, the user can type anything they want but i only want them to type numbers and not words.
And if they type words, it should errortrap it.
so how can i do that?
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Thu Jan 18, 2007 9:51 pm   Post subject: RE:errortrap

I believe strintok and strrealok will suit you perfectly.
nin




PostPosted: Thu Jan 18, 2007 9:59 pm   Post subject: Re: errortrap

wats that?
Expirant




PostPosted: Thu Jan 18, 2007 10:25 pm   Post subject: Re: errortrap

Press F10 while in Turing.

But basically strintok (x:string) is a function that returns whether 'x' is in the correct format for an integer type, and strreal does the same thing but for real numbers.

Ex:

code:

var s : string := "Hello"
var n : string := "5"
var r : string := "5.55"

put strintok (s)
put strrealok (s)
put strintok (n)
put strrealok (r)


So to check if an answer to a question is in the correct format (a.k.a an integer) use:

code:

var answer : string

put "What is 5 + 5?"
get answer : * % Numbers are < one word, but the  : * will keep the user from skipping lines

if strintok (answer) and strint (answer) = 10 then
    put "Correct!"
end if


Hope that clears it up,
Expirant
nin




PostPosted: Thu Jan 18, 2007 11:22 pm   Post subject: Re: errortrap

n if u were keppomg trck of the score how would u do that?
Expirant




PostPosted: Fri Jan 19, 2007 10:54 am   Post subject: Re: errortrap

Well, think about it. When does their score increase? When they get an answer correct, right? So...where in your code does it check if the answer is correct?
So if they get the answer correct, increase the score by 1. At that point in your code, you would have score += 1 (or score := score + 1). In the code I wrote, it would be:

code:

var answer : string
var score := 0

put "What is 5 + 5?"
get answer : *

if strintok (answer) and strint (answer) = 10 then
    put "Correct!"
    score += 1
end if

put "Final score: ", score


...

Think about it for a while, and if you're still having trouble, ask again.
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  [ 6 Posts ]
Jump to:   


Style:  
Search: