string to int

Discussion in 'C++' started by Beginner, Sep 11, 2005.

  1. Beginner

    Beginner Guest

    can you convert a string to an int in c++?

    string temp;
     
    Beginner, Sep 11, 2005
    #1
    1. Advertising

  2. Beginner

    Paul Guest

    Use the atoi function:
    int atoi(const char *string);

    Example:
    int n=atoi(temp.c_str());
     
    Paul, Sep 11, 2005
    #2
    1. Advertising

  3. Beginner

    Jack Klein Guest

    On 10 Sep 2005 22:05:56 -0700, "Paul" <> wrote in
    comp.lang.c++:

    > Use the atoi function:
    > int atoi(const char *string);
    >
    > Example:
    > int n=atoi(temp.c_str());


    Please don't ever recommend the ato... functions to anyone in C or
    C++. They produce undefined behavior if the conversion results in a
    value outside of the range of the destination type. Also, they do not
    provide a way to tell if a returned value of 0 is due to a string
    representing the value 0 or an invalid string that could not be
    converted at all.

    The standard library strto... functions were added a long time ago to
    overcome these problems. They have defined behavior with any input,
    other than a null pointer.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Sep 11, 2005
    #3
  4. Beginner

    darek Guest

    Beginner napisa³(a):
    > can you convert a string to an int in c++?
    >
    > string temp;
    >
    >
    >

    For example:

    #include <iostream>
    #include <sstream>

    int main( )
    {
    std::string a("321");
    int b;
    std::stringstream ss(a);
    ss >> b;
    std::cout << b;
    return 0;
    }
     
    darek, Sep 11, 2005
    #4
  5. Beginner

    Salil Guest

    You can declare a string say 6 bytes in length and insert all the
    digits of the number one at a time into the string.
    For example :
    #include<iostream.h>
    int main()
    {
    char str[6];
    int a=12345;
    for(int i=0;a!=0;i++)
    {
    str=a%10;
    a=a/10;
    }
    //this will make str : 54321
    //after that u can reverse the string using strrev()
    //and that's it!
    return 0;
    }
     
    Salil, Sep 11, 2005
    #5
  6. Beginner

    Peter_Julian Guest

    "Salil" <> wrote in message
    news:...
    | You can declare a string say 6 bytes in length and insert all the
    | digits of the number one at a time into the string.
    | For example :
    | #include<iostream.h>

    #include <iostream>

    | int main()
    | {
    | char str[6];

    is not a string, its a char array[]

    | int a=12345;
    | for(int i=0;a!=0;i++)
    | {
    | str=a%10;
    | a=a/10;
    | }
    | //this will make str : 54321
    | //after that u can reverse the string using strrev()
    | //and that's it!
    | return 0;
    | }

    You should study the std::string type and the std::stringstream classes.
    These dynamic containers include powerful algorithms, iterators and
    overloaded operators.

    You'll never, ever use a char array again except when absolutely
    neccessarry.
     
    Peter_Julian, Sep 11, 2005
    #6
  7. Salil wrote:
    >
    > You can declare a string say 6 bytes in length and insert all the
    > digits of the number one at a time into the string.
    > For example :
    > #include<iostream.h>
    > int main()
    > {
    > char str[6];
    > int a=12345;
    > for(int i=0;a!=0;i++)
    > {
    > str=a%10;
    > a=a/10;
    > }
    > //this will make str : 54321


    Aehm. No. This will not make str "54321" no most systems.

    > //after that u can reverse the string using strrev()
    > //and that's it!
    > return 0;
    > }



    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 12, 2005
    #7
  8. Beginner

    Salil Guest

    Thanks for pointing that out,
    I have corrected the code , it should be :
    #include<iostream.h>
    #include<string.h>
    int main()
    {
    char str[6]=" ";
    int a=12345;
    for(int i=0;a!=0;i++)
    {
    str=a%10;
    a=a/10;
    str+=48;
    }
    cout<<"\nNew string :"<<str;
    //str will be 54321
    char *stn=&str[0];
    strrev(stn);
    cout<<"\nReversed string :"<<str;
    //here str is reversed and it becomes : 12345
    return 0;
    }

    please note that this program was written in ANSI C++ , that's why i
    used the char array
    ~Salil
     
    Salil, Sep 12, 2005
    #8
  9. Salil wrote:
    >
    > Thanks for pointing that out,
    > I have corrected the code , it should be :


    You have corrected it for the case that the OP's system
    uses ASCII (most, but not all, do).

    Now I show you how you can achieve the same thing which
    works in any case, no matter if the OP'S system uses
    ASCII or not (Just one line needs to be changed)

    Instead of
    > str+=48;


    you write

    str += '0';

    This also has the benefit, that it gets clearer
    what is going on.


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 14, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,220
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    457
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    957
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    435
    James Kanze
    Apr 17, 2007
Loading...

Share This Page