Thread: converting from a string to an int

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    11

    converting from a string to an int

    hey everyone im having this little problem. Im trying to convert from a string to an int. ive tried to use atoi but that didnt work. that only works if it were char string[1] = "1"; int x; x = atoi(string);
    however in my situation, i have something delcared as a string like this:

    string x = "1";
    int y;

    y = atoi(x) // Will Not Work

    my friend told me something like s.at() would work, but i dont know how to use that, so someone please help because this program is due tomorrow. thanx

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Greetings,

    That is because 'atoi' is expecting a C string.
    There are at least a couple of ways you could do this, first by using the 'c_str()' member of std::string and pass that to 'atoi' like 'y = atoi(x.c_str());', this should work, although i preferer to use stringstreams because the 'atoX' family isn't part of the standart, i think. Besides, stringstreams are much sexyer!
    Stringstreams version:
    Code:
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int to_int(const string& val)
    {
        int number;
        ostringstream os(val);
        os >> number;
        return number;
    }
    
    // Example of how to use:
    int i = to_int("123");
    or 
    string x = "123";
    int i = to_int(x);

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    atoi() is ANSI C compatible, but not as sexy
    Also, you need to use istringstream, since you are getting input from the supplied string and putting it into an int.

    Just for fun, I traced to the execution alway down to a strtol(), which is also ANSI C compatible.

    gg

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    You are absolutely right about the 'istringstream', my bad!
    And I was wrong, again, about 'atoi', it is 'itoa' that is not part of the standart.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    i couldnt get either to work

    what im tryin to do in my program is that i have a binary tree, and when there is a number in the binary tree, i want to take that number, and convert it into an int and store it in a array and all the values are of string type.

    so lets say your thing is nodePtr->value, well see i tried coding to_int and havin it go x = to_int(nodePtr->value), but tha twouldnt work, and i tried going strtol, strtol(nodePtr->value), but that wouldnt work either. please help, thanx

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What type is the value member?

    gg

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Well, there was an error with my 'to_int' function, as Codeplug pointed out. Try this:
    Code:
    int to_int(const string& val)
    {
        int number;
        istringstream is(val);
        is >> number;
        return number;
    }
    This should work now.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    still doesnt work, i still get 2 errors, here they are

    C:\CECS328\bintree.cpp(652) : error C2079: 'is' uses undefined class 'basic_istringstream<char,struct std::char_traits<char>,class std::allocator<char> >'

    this one is reffering to is>>number


    C:\CECS328\bintree.cpp(652) : error C2440: 'initializing' : cannot convert from 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'int'

    and this one is referring to istringstream is(val) heres what i did


    else
    {

    x = to_int(nodePtr->value);
    return newvalues[ x-1 ];

    }


    i dunno whats wrong with it, x is an int, and nodePtr->value is a string value. its not char string, its simply string. thanx.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Do this:
    Code:
    #include<stdlib.h>
    ...
    strtol(nodePtr->value.c_str())
    Or to get to_int() to work, add this:
    Code:
    #include <string>
    #include <sstream>
    using namespace std;
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM