Author |
Message |
cycro1234
![](http://www.inetresults.com/gifs/images/Mad_hack.gif)
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Hikaru79
|
Posted: 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
|
|
|
|
|
|
![](images/spacer.gif) |
cycro1234
![](http://www.inetresults.com/gifs/images/Mad_hack.gif)
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
cycro1234
![](http://www.inetresults.com/gifs/images/Mad_hack.gif)
|
Posted: Thu Nov 25, 2004 10:03 pm Post subject: (No subject) |
|
|
Nvm, i think i got it. THX A LOT! |
|
|
|
|
![](images/spacer.gif) |
cycro1234
![](http://www.inetresults.com/gifs/images/Mad_hack.gif)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: 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, "."
|
|
|
|
|
|
![](images/spacer.gif) |
beard0
![](http://ugray.be/compsci/silly.gif)
|
Posted: 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 |
|
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Sat Nov 27, 2004 10:27 am Post subject: (No subject) |
|
|
ah hell 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) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
con.focus
![](http://C:\Documents and Settings\Presario\My Documents\My Received Files/fork.jpg)
|
Posted: Sun Nov 28, 2004 7:32 pm Post subject: (No subject) |
|
|
uh oh |
|
|
|
|
![](images/spacer.gif) |
con.focus
![](http://C:\Documents and Settings\Presario\My Documents\My Received Files/fork.jpg)
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
beard0
![](http://ugray.be/compsci/silly.gif)
|
Posted: 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)? |
|
|
|
|
![](images/spacer.gif) |
|