Computer Science Canada

change string to int

Author:  appling [ Thu Feb 05, 2004 4:13 pm ]
Post subject:  change string to int

what should i do if i have a string varable with 249.23 stored in it, and i want to times that number with 1.07???

Author:  recneps [ Thu Feb 05, 2004 4:28 pm ]
Post subject: 

use strreal (or streal?)

eg
code:

var var1:int
var1:=round(thestrreal(thestringvar)*1.07)

the strreal changes str to real, then multiplies by 1.07, and then is rounded off so it can be an int. if you dont want int, just real, make var1 a real var and get rid of round()

Author:  appling [ Thu Feb 05, 2004 4:35 pm ]
Post subject: 

i need to input the price from a txt file and calculate the gst.for this i can only declare the variable as string.

Author:  Martin [ Thu Feb 05, 2004 4:45 pm ]
Post subject: 

Suppose that you have something like this:


code:
var s : string := "43"
var n : int

n := strint (s)


strint converts a string to an integer. Take this case however:
code:
var s : string := "hello"
var n : int
n := strint (s)


This program would crash, because s can't be converted into an integer. To check to see if a variable can be converted to an integer, use strintok(string). If it can be converted, it will return true, if it can't, it will return false.

code:
var s : string
var n : int
put "Enter a number: "..
get s
if strintok (s) = true then
n := strint (s)
put "Two times your number is "..
put 2*n
elsif strintok (s) = false then
put "You didn't enter a number."
end if

Author:  appling [ Thu Feb 05, 2004 4:55 pm ]
Post subject: 

i try that but turing said -- illegal character in string passed to "strint"

Author:  Cervantes [ Thu Feb 05, 2004 5:04 pm ]
Post subject: 

darknesses code works fine for me. what version of Turing are you using? I'm on 4.0.5.

also are you using the last section of code that darkness posted? if so, post your code and we can have a looksie. Smile

Author:  appling [ Thu Feb 05, 2004 6:10 pm ]
Post subject: 

i am using 4.0.4c
this is my code
code:

var line : string
var streamIn : int
var price : string
var cost : real

open : streamIn, "iii.txt", get
assert streamIn > 0

put "ITEMS      " ..
put "COST"
put "---------------"
locate(3,1)
for i : 1 .. 2
    get : streamIn, line, price : *
    cost := strint (price)
    put line, "   ", price
end for


get : streamIn, line, price : *
    put line, "     ", price
get : streamIn, line, price : *
    put line, "    ", price



this is the iii.txt
code:

sweater 45.99
t-shirt 25.99
dress 80.99
shoes 102.00
[/quote]

Author:  Andy [ Thu Feb 05, 2004 7:36 pm ]
Post subject: 

ummm dude, thats not a frigging int, do u not read? you have to use strreal

Author:  appling [ Thu Feb 05, 2004 8:39 pm ]
Post subject: 

thanks i get that. Very Happy Very Happy









this really is a good place Shocked Very Happy Shocked


: