
-----------------------------------
Reality Check
Mon Dec 11, 2006 9:55 pm

Removing comma's from and integer?
-----------------------------------
Well, I'm writing a program that takes in a bunch of input but the input is to be seperated by a comma.  I know how to get rid of the comma if it is a string but when it is in a integer it quickly returns an error.

So for example the input is 1, 4, 9, 1.  How would I go about just taking the individual integers and ignoring the commas.  I thought about taking the numbers as strings but I'm going to be adding and multiplying the numbers so that won't work.  And sadly, I can't leave out the comma's :(

-----------------------------------
Clayton
Mon Dec 11, 2006 10:15 pm


-----------------------------------
then take it in as a string, take out the commas and then convert the strings to integers. For this you should check out strint and strintok

-----------------------------------
[Gandalf]
Tue Dec 12, 2006 7:52 pm


-----------------------------------
To remove the commas you'll need to use some string manipulation.  Basically you'll be iterating through each letter in the string and removing it if it's a comma, and then assigning the equivalent number to an integer variable.  The two following examples demonstrate everything you will need, it's up to you to put it all together though!
var name : string := "aGandalfa"
for i : 1 .. length (name)
    if name (i) = "a" then
        put "The letter at index ", i, " is an 'a'"
    end if
end for
var name := "Gandalf"
var nameExcludingThirdLetter := name (1 .. 2) + name (4 .. *)
put name
put nameExcludingThirdLetter

-----------------------------------
Reality Check
Wed Dec 13, 2006 12:37 am


-----------------------------------
Thanks a lot Gandalf but I won't need to do all that.  The variable inputted will always be one number followed by a comma so I know that the comma will always be the second character in the string.  Thanks though.

-----------------------------------
Reality Check
Wed Dec 13, 2006 5:27 pm


-----------------------------------
So sorry guys but I need help again.  I can't seem to convert or assign the right value to my int.  So if they were to type 1, and I remove the comma, I now have a '1' for my string.  I know I could just easily make a new integer variable and make it = to 1 but I can't think of an easy way to check what their string is.  I can't just type: 

if stringnumber := "1" then
integernumber := 1



I know that'll work but what if she types like a bigger number...and I can't get string to work either.  Sorry if I'm bugging you guys  :(

-----------------------------------
[Gandalf]
Wed Dec 13, 2006 6:16 pm


-----------------------------------
No problem. :)

In Turing, to convert something from one type to it's equivalent in another type you use functions like intstr(), strint(), realstr(), and so on.

For example:
var five : string := "5"
var numFive : int := strint (five)
put numFive

-----------------------------------
Reality Check
Wed Dec 13, 2006 6:45 pm


-----------------------------------
Thanks alot man.  I really appreciate the help.  That saved me a TON of time.  I thought I might have to hard code that part!
