Computer Science Canada string to number conversion |
Author: | Drakonius [ Mon Apr 14, 2003 10:17 am ] |
Post subject: | string to number conversion |
i'm a computer moron 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? |
Author: | Blade [ Mon Apr 14, 2003 10:52 am ] | ||
Post subject: | |||
use the variable as a string.. then convert it to a 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 |
Author: | Tony [ Mon Apr 14, 2003 3:43 pm ] | ||
Post subject: | |||
well if its a single digit, you can do
but if its a multidigit number, you'd have to go with blade's method |
Author: | Drakonius [ Tue Apr 15, 2003 9:53 am ] |
Post subject: | |
Anyway thxs... |
Author: | Leafs Fan [ Tue Apr 15, 2003 8:38 pm ] |
Post subject: | |
var num : string var inum : int var check : boolean := true loop loop colour (black) put "Enter the number --->" .. get num check:=true for x : 1 .. length (num) if num (x) = "1" or num (x) = "2" or num (x) = "3" or num (x) = "4" or num (x) = "5" or num (x) = "6" or num (x) = "7" or num (x) = "8" or num (x) = "9" or num (x) = "0" then if check not= false then check := true end if else colour (brightred) if check = true then put "Please enter a postive integer not anything else" delay (2500) cls end if check := false end if end for exit when check = true end loop inum := strint (num) if inum < 0 then put "Error please enter a postive integer" end if exit when inum > 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 |
Author: | jamez [ Tue Apr 15, 2003 8:58 pm ] | ||
Post subject: | |||
|
Author: | DarkHelmet [ Tue Apr 15, 2003 9:12 pm ] |
Post subject: | |
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. |
Author: | Leafs Fan [ Tue Apr 15, 2003 9:42 pm ] |
Post subject: | |
I tried that code out in turing and it come up with a lot of errors. What version of turing u got? |
Author: | Tony [ Tue Apr 15, 2003 10:16 pm ] |
Post subject: | |
lol, what DarkHelmet is saying, use getch() to input digits and check each one that its actually a digit but he's also right about strintok. The function is already writen for you. |
Author: | Drakonius [ Thu Apr 17, 2003 2:09 pm ] |
Post subject: | |
thx for the help with some slight modifications it fits right into my program |
Author: | Office of the Registar [ Thu Apr 17, 2003 2:28 pm ] |
Post subject: | |
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. |