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.
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.
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
you could mess around with the code so if isalpha() returns false then it asks for the users input again or whatever.Code:for (int i=0; i<=strlen(string); i++) { if (!isalpha(string[i])) { cout<<not an integer; } }
Ben.
Last edited by UnclePunker; 09-16-2003 at 03:48 AM.
Compiler == Visual C++ 6.0"Come Out Fighting."
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...
>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;
>>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]
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."
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.
>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.
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!
>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.