Computer Science Canada

if answer not = integer then?

Author:  Jas [ 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?

Author:  Mr. T [ Mon Mar 28, 2005 2:32 am ]
Post 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

Author:  Cervantes [ Mon Mar 28, 2005 6:11 am ]
Post 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

Author:  Mr. T [ Mon Mar 28, 2005 8:06 am ]
Post subject: 

ok, listen to cervantes, he knows best Laughing

Author:  Mazer [ Mon Mar 28, 2005 9:29 am ]
Post 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)

Author:  Jas [ Mon Mar 28, 2005 12:06 pm ]
Post 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.

Author:  Mazer [ Mon Mar 28, 2005 12:09 pm ]
Post subject: 

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

Author:  Naveg [ Mon Mar 28, 2005 5:44 pm ]
Post 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


: