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

Username:   Password: 
 RegisterRegister   
 if answer not = integer then?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Jas




PostPosted: Mon Mar 28, 2005 1:52 am   Post subject: if answer not = integer then?

Im doing a project, and my teacher said you have to make your program error trapped. So, i have a program where the user types an integer, and the program runs, but if he types anything else, it crashes. how do u error trap this? im asking for something like this...

var ans: int
get ans
if answer > 0 then
%continue with program
else ans not = int then
put "invalid input"
end if


the "else ans not = int then" line doesn't work, I just made it up so you will understand what im taking about. so what can i do to this program so it doesn't crash when the user types anything but an integer?

p.s. how do u put the "code" thing on top of your program? I saw other programs that people posted, and they had that word on top of them. why do u need that?
Sponsor
Sponsor
Sponsor
sponsor
Mr. T




PostPosted: Mon Mar 28, 2005 2:32 am   Post subject: (No subject)

[ code][/code ]
put ur code in between those tags. (but without the spaces in the [ ] brackets)

as for your int problem...as far as i know, that wont work because int is an identifier, and ur setting it as a condition. so really i have no solution Embarassed
Cervantes




PostPosted: Mon Mar 28, 2005 6:11 am   Post subject: (No subject)

String manipulation. I just remembered I'm going to write a tutorial on it soon, so if you don't know much about it, look for that.
Turing:

var input : string
var num : int
get input
if strintok (input) then
    num := strint (input)
end if
Mr. T




PostPosted: Mon Mar 28, 2005 8:06 am   Post subject: (No subject)

ok, listen to cervantes, he knows best Laughing
Mazer




PostPosted: Mon Mar 28, 2005 9:29 am   Post subject: (No subject)

You may also want to put that in a loop with a message to the user, as the rest of the program probably relies on the variable:
Turing:

var input : string
var num : int
get input
loop
    exit when strintok (input)
    put "Invalid input, please try again:"
    get input
end loop
num := strint (input)
Jas




PostPosted: Mon Mar 28, 2005 12:06 pm   Post subject: (No subject)

I see how this works. I decided to use something like this:

code:


var input : string
var num : int
loop
    get input
    if strintok (input) then
        num := strint (input)
    else
        put "Invalid input"
    end if
end loop



thx for the help.
Mazer




PostPosted: Mon Mar 28, 2005 12:09 pm   Post subject: (No subject)

Don't forget to exit the loop once you've got a valid input!
Naveg




PostPosted: Mon Mar 28, 2005 5:44 pm   Post subject: (No subject)

Jas wrote:
I see how this works. I decided to use something like this:

code:


var input : string
var num : int
loop
    get input
    if strintok (input) then
        num := strint (input)
        exit
    else
        put "Invalid input"
    end if
end loop



thx for the help.


there i added a line, dont forget it its important
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: