
-----------------------------------
Chris
Sat Jun 10, 2006 11:44 am

String to int
-----------------------------------
I was wondering if there is anyway of changing a string value to an int value. Thanks for any help in advance.

-----------------------------------
wtd
Sat Jun 10, 2006 11:54 am


-----------------------------------
Stringstreams can do the job reasonably well.

-----------------------------------
Chris
Sat Jun 10, 2006 12:11 pm


-----------------------------------
Stringstreams can do the job reasonably well. Can you explain this a little more.. thanks

-----------------------------------
wtd
Sat Jun 10, 2006 1:37 pm


-----------------------------------
Google for "c++ stringstream".

I'm happy to answer questions, but you should do some of your own research first.

-----------------------------------
Panopticon
Sun Aug 06, 2006 12:33 am


-----------------------------------
atoi(); works fine. [url=http://man.he.net/man3/atoi] Man page for said function

-----------------------------------
Andy
Sun Aug 06, 2006 10:39 am


-----------------------------------
atoi is used for character arrays, converting a string adt to an character array then passing it through atoi is no where near as efficient as using the stringstream approach wtd suggested

-----------------------------------
Panopticon
Sun Aug 06, 2006 11:23 am


-----------------------------------
atoi is used for character arrays, converting a string adt to an character array then passing it through atoi is no where near as efficient as using the stringstream approach wtd suggested

I assumed that he meant character arrays when he said string. If the string is in an ADT then you're right: using atoi is quite inefficient.

-----------------------------------
bugzpodder
Sun Aug 06, 2006 1:23 pm


-----------------------------------
atoi is used for character arrays, converting a string adt to an character array then passing it through atoi is no where near as efficient as using the stringstream approach wtd suggested
string objects probably use internal char array representation, so it is pretty efficient.

-----------------------------------
bugzpodder
Sun Aug 06, 2006 1:24 pm


-----------------------------------
and stringstream probably have a lot more overhead than atoi

-----------------------------------
wtd
Sun Aug 06, 2006 1:34 pm


-----------------------------------
and stringstream probably have a lot more overhead than atoi

Well, a lot of this depends on the language you're using.  

Using C?  Then you can't use stringstream.  

Using C++?  Then you should be using stringstream.

-----------------------------------
UnsignedChar
Mon Dec 18, 2006 4:29 pm


-----------------------------------
How about just multiplying by 10 and subtracting the character value?
Like in this example:


#include 
#include 
        
int main(void)
{       
        char str[] = "123456789";
        int  num = 0, i;
                                 
        for(i = 0; i < strlen(str); i++){
                num *= 10;
                num += (str[i] - '0');
        }       
                         
        printf("%d", num);                
        return 0;
}

-----------------------------------
Monstrosity_
Mon Dec 18, 2006 4:46 pm


-----------------------------------
atoi(); works fine. 
No it doesn't, even if he were using an array of characters.

#include 
#include 
       
int main(void)
{       
        char str
This is fine until you let the user enter some data. What if its negative, contains non-numeric characters, overflows, ect..

-----------------------------------
Clayton
Mon Dec 18, 2006 4:54 pm


-----------------------------------
Holy necro-post Batman!

Welcome to CompSci.ca UnsignedChar! It's nice to see you contributing, but this topic is over 3 months old. Just be sure to check the dates next time :D

Also, like Monstrosity_ said, what if the user enters some weird output? Then you get an error, not good.

-----------------------------------
UnsignedChar
Mon Dec 18, 2006 6:14 pm


-----------------------------------

Welcome to CompSci.ca UnsignedChar!


Thank you.


It's nice to see you contributing, but this topic is over 3 months old. Just be sure to check the dates next time :D


No problem, I can deal with that....only realize that i can go back two pages worth of topics and hit last year, it'll be hard not to break this rule.

Also, (not trying to be a jerk) but topics that are old does not mean they can not be contributed to further, perhaps if the topic were a dead or solved subject then it would be inappropriate.


Also, like Monstrosity_ said, what if the user enters some weird output? Then you get an error, not good.


Here's a good example of my point, this topic is now extended by some interesting questions.
