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

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




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Hikaru79




PostPosted: Thu Nov 25, 2004 9:51 pm   Post subject: (No 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
cycro1234




PostPosted: Thu Nov 25, 2004 10:00 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 25, 2004 10:03 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 25, 2004 10:16 pm   Post subject: (No 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.
Cervantes




PostPosted: Fri Nov 26, 2004 4:25 pm   Post subject: (No 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, "."
beard0




PostPosted: Fri Nov 26, 2004 7:26 pm   Post subject: (No 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
Cervantes




PostPosted: Sat Nov 27, 2004 10:27 am   Post subject: (No 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)
Sponsor
Sponsor
Sponsor
sponsor
con.focus




PostPosted: Sun Nov 28, 2004 7:32 pm   Post subject: (No subject)

uh oh
con.focus




PostPosted: Sun Nov 28, 2004 7:34 pm   Post subject: (No 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
beard0




PostPosted: Sun Nov 28, 2004 10:18 pm   Post subject: (No 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)?
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  [ 11 Posts ]
Jump to:   


Style:  
Search: