[n00b]Code on how to not crash getting real if string input
Author |
Message |
Nigrev
|
Posted: Fri Nov 17, 2006 9:27 pm Post subject: [n00b]Code on how to not crash getting real if string input |
|
|
I've searched, or at least tried to, look for this code.
I have a variable declared as real. Then I have the get command.
Now I don't want the program to crash if some monkey inputs a letter, neither do I want the cursor to go to the next line if they press the enter key. Any work-arounds would be nice, but if there is a specific command, then better.
I will need this variable for later calculations, so i cannot use a string var. This is a part of a program due in 2 days for my tech class. I am at foolproofing stage.
Most efficient coding please. This is the only site I know at the moment that would help me. All help appreciated. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Nova
|
Posted: Fri Nov 17, 2006 10:00 pm Post subject: (No subject) |
|
|
To fix the crash when letters are typed in instead of a get command you could use a looped Input.KeyDown and ignore all but numbers
For the skipping of a line you can locate after the length of the input
And by the way, you can use string as all you need to do is strreal () which will change the string to real |
|
|
|
|
![](images/spacer.gif) |
Wolf_Destiny
![](http://compsci.ca/v3/uploads/user_avatars/152219065473127fae8c5e.jpg)
|
Posted: Fri Nov 17, 2006 10:37 pm Post subject: (No subject) |
|
|
Foolproofing huh? Well then if you've been reading the textbook like they tell you (yawn!) then you could (depending on which textbook) find some reference to the commands: strint,intstr,strintok,strreal,realstr,strreakok.
Looking at the help for those commands should help immensely. But what you should get from them is that you can convert string to real and vice-versa.
So what does this mean?
You can use a string variable in your get statement. Then check to see if any monkeys pressed a letter. If they didn't, then convert your string to a real number.
code: | var line : string
put "Enter a number!"
put "Number: "..
get line
var number : real
if strrealok (line) then
number := strreal (line)
else
put "No letters or symbols you monkey!"
end if
|
Then you could add a loop to it and make it exit when strrealok.
Check the help files, do some reading if available, and good luck!
~Wolf_Destiny
P.S. Were these the monkeys? http://www.theharrowgroup.com/articles/20030915/20030915_files/image004.jpg
P.P.S. I don't even know, is that a mac mini or what? I suppose it would be, Windoze isn't really user-friendly enough, and when you're just trying to fit in with the other species you shouldn't have to put up with it. |
|
|
|
|
![](images/spacer.gif) |
Nigrev
|
Posted: Sat Nov 18, 2006 7:45 pm Post subject: (No subject) |
|
|
^thanx
...still can't fix my enter problem... ![Crying or Very sad Crying or Very sad](images/smiles/icon_cry.gif) |
|
|
|
|
![](images/spacer.gif) |
Wolf_Destiny
![](http://compsci.ca/v3/uploads/user_avatars/152219065473127fae8c5e.jpg)
|
Posted: Sat Nov 18, 2006 9:22 pm Post subject: (No subject) |
|
|
Text.WhatRow
Text.WhatCol
Try storing the Row and Column numbers, then locating back to them after getting the number and erasing it. OR if you want to display the number, then locate to where you started and put "the number"..
I'm never sure if I explain things well, and I'm pretty sure I don't, so...example!
Turing: | put "Number: " ..
loop
var row : int := Text.WhatRow
var col : int := Text.WhatCol
var number : real
get number
locate (row, col )
put " " %Clear the line to the end
locate (row, col )
put number, " " .. %Display the original number and a space
%The line was cleared because of Turing's tendency to use scientific notation
end loop |
~Wolf_Destiny |
|
|
|
|
![](images/spacer.gif) |
Nigrev
|
Posted: Sun Nov 19, 2006 10:09 pm Post subject: (No subject) |
|
|
actually...i mean...
if you run the program and not enter something at the prompt, and just press enter, the cursor goes to the next line. I want to prevent this. if you do not enter something and just press enter, i want the cursor to go back to where it started. |
|
|
|
|
![](images/spacer.gif) |
uberwalla
![](http://www.mac-help.com/images/starwars.gif)
|
Posted: Sun Nov 19, 2006 10:18 pm Post subject: (No subject) |
|
|
if u use the Text.Locate command then it will stay at the line u want im pretty sure. |
|
|
|
|
![](images/spacer.gif) |
Nigrev
|
Posted: Mon Nov 20, 2006 6:21 am Post subject: (No subject) |
|
|
I'm trying everything and nothing fits my situation....sorry.
This is sample what I have to work with...
code: |
var ca:string
var cash:real
put "Interest Calculation Table" : 22
put ""
loop
locate (3, 1)
put "Initial amount: $" ..
get ca %gets cash
if strrealok (ca) then
cash := strreal (ca) %sets string to real
else
put "Number input only" %error warning
locate (3, 1)
put "" %clears located line
end if
exit when strrealok (ca)
end loop |
why can't loop handle empty inputs?[/code] |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Prince Pwn
|
Posted: Mon Nov 20, 2006 8:22 am Post subject: (No subject) |
|
|
All I can think of to fix your problem is to use getchar and store each letter/number the person types in an array, so you can make an if statment that if "ENTER" is pressed you then check all the chars combined and see if it's strintok and if not it clears the screen and restarts a loop, if you can understand me. |
|
|
|
|
![](images/spacer.gif) |
Prince Pwn
|
Posted: Mon Nov 20, 2006 8:32 am Post subject: (No subject) |
|
|
Here I whipped up an example. This will not go down every time you enter an empty string. But you gotta add more inside the if statment if you want it to combine all your char's then do strintok.
code: |
%View.Set ("text")
var ca : array 1 .. 100 of char
var cnt : int := 0
var cash : real
put "Interest Calculation Table" : 22
put ""
loop
put "Initial amount: $" ..
loop
cnt += 1
ca (cnt) := getchar
if ca (cnt) = KEY_ENTER then
for i : 1 .. cnt
put ca (i) ..
end for
cnt := 0
locate (3, 18)
end if
end loop
end loop
|
|
|
|
|
|
![](images/spacer.gif) |
|
|