
-----------------------------------
Drakonius
Mon Apr 14, 2003 10:17 am

string to number conversion
-----------------------------------
i'm a computer moron :oops:  and trying to learn some basics of programming. When i use a textfield and trying to get a number from it...how do i make sure that the number is an actual number and not text, without turing giving an error message. Like how do i check ahead of time?
 :roll:

-----------------------------------
Blade
Mon Apr 14, 2003 10:52 am


-----------------------------------
use the variable as a string.. then convert it to a number...
var number:string
var num:int
put "enter a number"
get number
num:=strint(number)
thats the only thing i can think of for now.. but you will still get an error if it isnt a number and you try to assign it to an integer variable

-----------------------------------
Tony
Mon Apr 14, 2003 3:43 pm


-----------------------------------
well if its a single digit, you can do

if index("1234567890", digit)  0

    end loop


** here u go.. its hard for me to explain but basicially all u do is get it a string and then check each character and make sure that none of them are anything besides a number

-----------------------------------
jamez
Tue Apr 15, 2003 8:58 pm


-----------------------------------
var num : string
var inum : int
var check : boolean := false

loop
    colour (black)
    put "Enter the number --->" ..
    get num
    check := false
    for x : 1 .. length (num)
        if index ("0123456789", num (x)) < 1 then
            colour (brightred)
            put "Please enter a postive integer not anything else"
            check := true
            exit
        end if
    end for
    exit when check = false
end loop

inum := strint (num)

put "number is ",inum

-----------------------------------
DarkHelmet
Tue Apr 15, 2003 9:12 pm


-----------------------------------
you are making it more complicated than it has to be. Just use strintok. if it returns true, it is a proper number, if it returns false, it isn't a number. A slightly more complicated method is:

procedure getnum (var intnum : int, intx : int, inty : int)
    var strnum : string
    var chars : array char of boolean
    strnum := ""
    locate (intx, inty)
    setscreen ("cursor")
    loop
        Input.Pause
        Input.KeyDown (chars)
        locate (intx, inty)
        put skip
        locate (intx, inty)
        if chars ('1') then
            strnum := strnum + "1"
        elsif chars ('2') then
            strnum := strnum + "2"
        elsif chars ('3') then
            strnum := strnum + "3"
        elsif chars ('4') then
            strnum := strnum + "4"
        elsif chars ('5') then
            strnum := strnum + "5"
        elsif chars ('6') then
            strnum := strnum + "6"
        elsif chars ('7') then
            strnum := strnum + "7"
        elsif chars ('8') then
            strnum := strnum + "8"
        elsif chars ('9') then
            strnum := strnum + "9"
        elsif chars ('0') then
            strnum := strnum + "0"
        elsif chars (KEY_ENTER) then
            exit
        elsif chars (KEY_BACKSPACE) then
            if length (strnum) > 1 then
                strnum := strnum (1 .. (length (strnum) - 1))
            else
                strnum := ""
            end if
        end if
        put strnum ..
    end loop
    intnum := strint(strnum)
end getnum

This method makes it so that the program only responds if the user presses numbers. Any other digits don't show up in the final product. To run this, just type:

getnum(num, 10, 10)

the num is the integer you want the user to input. The other two are used so it knows where to locate it.

-----------------------------------
Leafs Fan
Tue Apr 15, 2003 9:42 pm


-----------------------------------
I tried that code out in turing and it come up with a lot of errors. What version of turing u got?

-----------------------------------
Tony
Tue Apr 15, 2003 10:16 pm


-----------------------------------
lol, what DarkHelmet is saying, use getch() to input digits and check each one that its actually a digit :P

but he's also right about strintok. The function is already writen for you.

-----------------------------------
Drakonius
Thu Apr 17, 2003 2:09 pm


-----------------------------------
thx for the help
with some slight modifications it fits right into my program :D

-----------------------------------
Office of the Registar
Thu Apr 17, 2003 2:28 pm


-----------------------------------
yeah, just use strintok, but it sort of redundent since you have to wait for the user to type enter/tab etc.

you should use a char variable to do that.
