Computer Science Canada

Removing comma's from and integer?

Author:  Reality Check [ Mon Dec 11, 2006 9:55 pm ]
Post subject:  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 Sad

Author:  Clayton [ Mon Dec 11, 2006 10:15 pm ]
Post subject: 

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

Author:  [Gandalf] [ Tue Dec 12, 2006 7:52 pm ]
Post subject: 

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

code:
var name := "Gandalf"
var nameExcludingThirdLetter := name (1 .. 2) + name (4 .. *)
put name
put nameExcludingThirdLetter

Author:  Reality Check [ Wed Dec 13, 2006 12:37 am ]
Post subject: 

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.

Author:  Reality Check [ Wed Dec 13, 2006 5:27 pm ]
Post subject: 

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

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 Sad

Author:  [Gandalf] [ Wed Dec 13, 2006 6:16 pm ]
Post subject: 

No problem. Smile

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:
code:
var five : string := "5"
var numFive : int := strint (five)
put numFive

Author:  Reality Check [ Wed Dec 13, 2006 6:45 pm ]
Post subject: 

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!


: