Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 change string to int
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
appling




PostPosted: 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???
Sponsor
Sponsor
Sponsor
sponsor
recneps




PostPosted: Thu Feb 05, 2004 4:28 pm   Post subject: (No 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()
appling




PostPosted: Thu Feb 05, 2004 4:35 pm   Post subject: (No 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.
Martin




PostPosted: Thu Feb 05, 2004 4:45 pm   Post subject: (No 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
appling




PostPosted: Thu Feb 05, 2004 4:55 pm   Post subject: (No subject)

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




PostPosted: Thu Feb 05, 2004 5:04 pm   Post subject: (No 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
appling




PostPosted: Thu Feb 05, 2004 6:10 pm   Post subject: (No 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]
Andy




PostPosted: Thu Feb 05, 2004 7:36 pm   Post subject: (No subject)

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




PostPosted: Thu Feb 05, 2004 8:39 pm   Post subject: (No subject)

thanks i get that. Very Happy Very Happy









this really is a good place Shocked Very Happy Shocked
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: