Computer Science Canada

if statements help

Author:  cycro1234 [ Thu Nov 25, 2004 9:29 pm ]
Post subject:  if statements help

Aight, so i'm learning this new thing in turing called "error trapping". Annoying lil bugger if u ask me, but important nonetheless. Neways, lets say I have an if statement for one value. And i want the program to end when the value is something other than the one i want. That is, ANY value, meaning string or real number. Example:

get x

if x = 5 then
put "<Insert witty comment here>"

now i need the code to make the program output the next part IF x is any value other than 5, including strings.

put "You will read this if you inputted any other value for x other than 5" put "including a string"

else would work, but only if the input is a real type. I need to cover ALL possiblities. Thx in advance

Author:  Hikaru79 [ Thu Nov 25, 2004 9:51 pm ]
Post subject: 

Your question is kinda convoluted and hard to understand. But I'll post an example error trapping statement and you can probably adapt it to your own use. Say you want the user to input an INTEGER. This is what you do:

code:

var userInput : string
loop
    put "Please enter an integer:  " ..
    get userInput
    if strintok (userInput) then
        put "Congratulations! You know what an integer is!"
    else
        put "You suck. That's not an int!"
    end if
end loop

Author:  cycro1234 [ Thu Nov 25, 2004 10:00 pm ]
Post subject: 

ok but what if i dont want to do it for an integer? What if i want to do it for a string?

Author:  cycro1234 [ Thu Nov 25, 2004 10:03 pm ]
Post subject: 

Nvm, i think i got it. THX A LOT!

Author:  cycro1234 [ Thu Nov 25, 2004 10:16 pm ]
Post subject: 

UGH! I didn't get it. I want the user to input a real number. If the user inputs a letter instead of a number, i want the program to tell the user to input again properly.

Author:  Cervantes [ Fri Nov 26, 2004 4:25 pm ]
Post subject: 

I'm not that good with string manipulation but here's my approach:
code:

var userInput : string

loop
    locate (1, 1)
    put "Enter a real number : " ..
    get userInput
    exit when index ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigjklmnopqrstuvwxyz,<>/?;':\"[]{}`~!@#$^&*()_+|-", userInput) = 0
    locate (1, 1)
    put repeat (" ", maxcol)
end loop
var realNum := userInput
put "You entered ", realNum, "."

Author:  beard0 [ Fri Nov 26, 2004 7:26 pm ]
Post subject: 

code:
var input : string
var num : real
loop
    put "Enter a real number: " ..
    get input
    cls
    exit when strrealok (input)
    put "That was not a real number!"
end loop
num := strreal (input)
put "The number entered is ", num

Author:  Cervantes [ Sat Nov 27, 2004 10:27 am ]
Post subject: 

Surprised ah hell Confused I didn't think there was a strrealok command. I must have typed strealok when I first tried it.
Given that there is a strrealok command, use beard0's code 8)

Author:  con.focus [ Sun Nov 28, 2004 7:32 pm ]
Post subject: 

uh oh

Author:  con.focus [ Sun Nov 28, 2004 7:34 pm ]
Post subject: 

shoot sry if u want it to end when it encounters sumthing else then use a loop and tell use the exit when command to tell it when to stop the loop

Author:  beard0 [ Sun Nov 28, 2004 10:18 pm ]
Post subject: 

con.focus: what does that code have to do with asking for numerical input, and allowing user stupidity (ie, they enter a non-number)?


: