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

Username:   Password: 
 RegisterRegister   
 String to int
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Chris




PostPosted: Sat Jun 10, 2006 11:44 am   Post subject: 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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Jun 10, 2006 11:54 am   Post subject: (No subject)

Stringstreams can do the job reasonably well.
Chris




PostPosted: Sat Jun 10, 2006 12:11 pm   Post subject: (No subject)

wtd wrote:
Stringstreams can do the job reasonably well.
Can you explain this a little more.. thanks
wtd




PostPosted: Sat Jun 10, 2006 1:37 pm   Post subject: (No subject)

Google for "c++ stringstream".

I'm happy to answer questions, but you should do some of your own research first.
Panopticon




PostPosted: Sun Aug 06, 2006 12:33 am   Post subject: (No subject)

atoi(); works fine. Man page for said function
Andy




PostPosted: Sun Aug 06, 2006 10:39 am   Post subject: (No subject)

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




PostPosted: Sun Aug 06, 2006 11:23 am   Post subject: (No subject)

Andy wrote:
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




PostPosted: Sun Aug 06, 2006 1:23 pm   Post subject: (No subject)

Andy wrote:
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.
Sponsor
Sponsor
Sponsor
sponsor
bugzpodder




PostPosted: Sun Aug 06, 2006 1:24 pm   Post subject: (No subject)

and stringstream probably have a lot more overhead than atoi
wtd




PostPosted: Sun Aug 06, 2006 1:34 pm   Post subject: (No subject)

bugzpodder wrote:
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




PostPosted: Mon Dec 18, 2006 4:29 pm   Post subject: (No subject)

How about just multiplying by 10 and subtracting the character value?
Like in this example:

code:

#include <stdio.h>
#include <string.h>
       
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_




PostPosted: Mon Dec 18, 2006 4:46 pm   Post subject: (No subject)

Panopticon wrote:
atoi(); works fine. Man page for said function

No it doesn't, even if he were using an array of characters.

Panopticon wrote:
#include <stdio.h>
#include <string.h>

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;
}

This is fine until you let the user enter some data. What if its negative, contains non-numeric characters, overflows, ect..
Clayton




PostPosted: Mon Dec 18, 2006 4:54 pm   Post subject: (No subject)

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 Very Happy

Also, like Monstrosity_ said, what if the user enters some weird output? Then you get an error, not good.
UnsignedChar




PostPosted: Mon Dec 18, 2006 6:14 pm   Post subject: (No subject)

Freakman wrote:

Welcome to CompSci.ca UnsignedChar!


Thank you.

Freakman wrote:

It's nice to see you contributing, but this topic is over 3 months old. Just be sure to check the dates next time Very Happy


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.

Freakman wrote:

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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: