integers as a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Vallely

    integers as a string

    If anyone could help me with my problem I'd greatly appreciate it, this
    question probably has a quick easy answer but I've been wanting to punch the
    wall for the last hour because of it.

    I have a string IDNUM = "12345";

    I need to do simple arithmetic with the integers in this string.

    The way I've been trying and failing is by doing something similar to this:



    int x , y;
    string i1=IDNUM.substr (0,1); //sets i1 = 1 hopefully
    x = atoi(i1); //Here, I fail to convert the integer in the string to
    the type integer
    y = x + 5;


    I know the "x = atoi(i1)" is the root of all of my problems. But I do not
    know how to fix it, and I have two books both of which do not touch on
    atoi( ) at all. (buying a new book right now isn't the most convenient
    option). At the time this section of code executes, the integers in the
    string could be anything, so I can't just plug in the numbers.

    So to sum up my question, I have a string of integers named IDNUM, and I
    cannot figure out how to use the integers in this string outside of
    outputting the string and then having the user re-enter the numbers they see
    into an INT variable, which is stupid. Thanks in advance for any help.

    -Mike V



  • Mike Vallely

    #2
    Re: integers as a string


    Nevermind, I just googled it and got the exact answer I needed. Probably
    should have done that beforehand.

    -Mike V


    Comment

    • Ali R.

      #3
      Re: integers as a string


      "Mike Vallely" <mkNOSPAMvallel y@hotmail.com> wrote in message
      news:HUSrb.2797 8$kS.13853@news svr31.news.prod igy.com...[color=blue]
      > If anyone could help me with my problem I'd greatly appreciate it, this
      > question probably has a quick easy answer but I've been wanting to punch[/color]
      the[color=blue]
      > wall for the last hour because of it.
      >
      > I have a string IDNUM = "12345";
      >
      > I need to do simple arithmetic with the integers in this string.
      >
      > The way I've been trying and failing is by doing something similar to[/color]
      this:[color=blue]
      >
      >
      >
      > int x , y;
      > string i1=IDNUM.substr (0,1); //sets i1 = 1 hopefully
      > x = atoi(i1); //Here, I fail to convert the integer in the string to
      > the type integer
      > y = x + 5;
      >
      >
      > I know the "x = atoi(i1)" is the root of all of my problems. But I do not
      > know how to fix it, and I have two books both of which do not touch on
      > atoi( ) at all. (buying a new book right now isn't the most convenient
      > option). At the time this section of code executes, the integers in the
      > string could be anything, so I can't just plug in the numbers.
      >
      > So to sum up my question, I have a string of integers named IDNUM, and I
      > cannot figure out how to use the integers in this string outside of
      > outputting the string and then having the user re-enter the numbers they[/color]
      see[color=blue]
      > into an INT variable, which is stupid. Thanks in advance for any help.
      >
      > -Mike V
      >
      >
      >[/color]

      The root of the problem is that you don't know how to use string.

      string str = "12345";
      int x = atoi(str.c_str( ));


      Comment

      • Tim Threlfall

        #4
        Re: integers as a string

        Hi

        atoi() takes a const char* as an argument, not an std::string. So you
        would need to do atoi(i1.c_str() );

        You could also use stringstreams instead of atoi() to convert an
        std::string into an int, something like this


        template<typena me RT, typename T, typename Trait, typename Alloc>
        RT ss_atoi( std::basic_stri ng<T, Trait, Alloc>& the_string )
        {
        std::basic_istr ingstream< T, Trait, Alloc> temp_ss(the_str ing);
        RT num;
        temp_ss >> num;
        return num;
        }

        Which you would call as int x = ss_atoi<int>(i1 );


        Or you could use boost::lexical_ cast.

        Comment

        Working...