Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
string to number conversion
Author Message
Drakonius

Posted: 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?

Posted: Mon Apr 14, 2003 10:52 am   Post subject: (No subject)

use the variable as a string.. then convert it to a number...
 code: 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

Posted: Mon Apr 14, 2003 3:43 pm   Post subject: (No subject)

well if its a single digit, you can do
 code: if index("1234567890", digit) <1 then put "not a digit" end if

but if its a multidigit number, you'd have to go with blade's method
Tony's programming blog. DWITE - a programming contest.
Drakonius

Posted: Tue Apr 15, 2003 9:53 am   Post subject: (No subject)

Anyway thxs...
Leafs Fan

Posted: Tue Apr 15, 2003 8:38 pm   Post subject: (No 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
jamez

Posted: Tue Apr 15, 2003 8:58 pm   Post subject: (No subject)

 code: 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

Posted: Tue Apr 15, 2003 9:12 pm   Post subject: (No 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.
Leafs Fan

Posted: Tue Apr 15, 2003 9:42 pm   Post subject: (No subject)

I tried that code out in turing and it come up with a lot of errors. What version of turing u got?

Tony

Posted: Tue Apr 15, 2003 10:16 pm   Post subject: (No 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.
Tony's programming blog. DWITE - a programming contest.
Drakonius

Posted: Thu Apr 17, 2003 2:09 pm   Post subject: (No subject)

thx for the help
with some slight modifications it fits right into my program
Office of the Registar

Posted: Thu Apr 17, 2003 2:28 pm   Post subject: (No 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.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 11 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: