converting a string to an integer

This is a discussion on converting a string to an integer within the C++ Programming forums, part of the General Programming Boards category; I need to convert a string (that is made up of all digits) to an integer so that I can ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    Question converting a string to an integer

    I need to convert a string (that is made up of all digits) to an integer so that I can manipulate in mathematically. I know I've done it before but can't find how I did it now.

  2. #2
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    You can use a function called atoi();
    you need to include stdlib.h and it works like this

    integer = atoi(string);

    hope this helps

    It stands for alpha to integer.

    For security it would be good to make sure that every element of your character array is definitely a number, you can use a function called isalpha(); for that

    Code:
    for (int i=0; i<=strlen(string); i++)
    {
         if (!isalpha(string[i]))
         {
               cout<<not an integer;
         }
    }
    you could mess around with the code so if isalpha() returns false then it asks for the users input again or whatever.

    Ben.
    Last edited by UnclePunker; 09-16-2003 at 04:48 AM.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    Few days ago I have found another way by reading Linux manpage: you can use function strtol(), which will also report for what reason the conversion failed (if it failed). See man strtol for better description, now I don't have Linux installed here.
    The former way (atoi()), which is a macro, I think, is probably more standard. Or at least widely used in tutorials...

  4. #4
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >now I don't have Linux installed here.
    strtol is standard.

    >The former way (atoi()), which is a macro, I think, is probably more standard.
    Where did you read that atoi is a macro? But it's just as standard as strtol, which is a pity because if any error happens atoi is unpredictable. strtol is way better:
    Code:
    char *sv;
    char *loc;
    long rv;
    int dv;
    
    ...
    
    rv = strtol(sv, &loc, 0);
    if (loc == sv)
      cerr<<"No numbers to convert"<<endl;
    else if (rv > INT_MAX || INT_MIN > rv)
      cerr<<"Out of range"<<endl;
    else
      dv = (int)rv;

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I need to convert a string (that is made up of all digits) to an integer
    You could always try reading the FAQ
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    It seems that strtol() converts to a long and not an integer and that is what he asked for, and as atoi() does fail if it doesn't get what it expects I pointed him in the direction of doing some simple error handling, I would say that was far better than strtol().

    Ben.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    Thanks guys! Yes, before the conversion I check the user's input to make sure that each character is a digit. I've used atoi() before but was thinking that you passed it a character and not a string.
    I'm going to give it a try, thanks again.

  8. #8
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >It seems that strtol() converts to a long and not an integer and that is what he asked for
    Casting to a narrower type is perfectly legal you know. It's also a simple way to check for overflow if you need to do that. I described such a case above.

    >I pointed him in the direction of doing some simple error handling
    I pointed him in the direction of doing some good error handling. Simple goes splat pretty quickly in nontrivial programs. What I showed wasn't even bulletproof, you can use strtol and have secure and concise code. Show me *that* with your validation routines and atoi.

    >I would say that was far better than strtol().
    What if he wants to still attempt a conversion if the first few characters of the string are digits? Something like "1024askldjfasldkjf" being treated as valid input if the parser ignores everything after the last digit isn't unheard of. Oh yea, what if there's more to the string than just a number and you want all of it. strtol's endp parameter can look mighty tasty when you need to parse a string in sequence.

    Not that I'm trying to flame you to ash (just nicely crispy), it's just that we should let the obsolete nasties die a quiet death instead of try to revive them. Stuff like gets and atoi and their buddies have no place in modern code.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    thanks twm,
    I was looking for a string function that would do what atoi does. It seemed logical to have one, but couldn't find it in my book!

  10. #10
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >but couldn't find it in my book!
    It's good to get a tutorial book and a reference book, that way you don't get one that incompetently tries to be both. It's really hard to do it right (like K&R). But, for the majority of your C++ needs, The C++ Programming Language by Bjarne Stroustrup is what you need.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Converting a string to an integer
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2001, 11:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21