Convert string to int using C++

The string class is a very useful way to deal with strings when using c++. Sometimes, however, you run into conversion issues and you want to be able to convert a string which contains an integer into an int datatype. The trick to doing this conversion is simply to use the function atoi found in stdlib.h. But you must pass that function a char array and therefore you must use the c_str() method to convert your string class to a character array.

The following code is an example of how you can convert string to int.

#include <stdlib.h>
#include <iostream.h>
#include <string>

int GetIntVal(string strConvert) {
  int intReturn;

  // NOTE: You should probably do some checks to ensure that
  // this string contains only numbers. If the string is not
  // a valid integer, zero will be returned.
  intReturn = atoi(strConvert.c_str());

  return(intReturn);
}

That's that. Your string is now converted into an int.


Author: DPAK
Created: Dec 1 2005
Categories: C++
TechByte #99

Warning: By visiting this site and/or by using any information contained herein, you agree to the Techbytes.ca terms of use.


Comment posted by 'Anonymous' on Mar 31 2006 @ 21:11:42
Thanks, worked fine!
Comment posted by 'Yoav Aminov' on Jun 10 2007 @ 23:46:55
Thank you very much! you are a king!!
Comment posted by 'James Bond' on Jun 17 2007 @ 03:51:39
Finally a function that works!! You da Man!!!
Comment posted by 'Evil Twin' on Jul 11 2007 @ 09:46:18
Should be mentioned however that this isn't a portable way of doing it. This works only in windows environments.
Comment posted by 'Anonymous' on Jul 27 2007 @ 08:25:21
This is the standard c way of doing it (works on any platform with the standard c-library). The c++ way should be:

using namespace std;
string s = "123";
int i;
bool success;
istringstream myStream(s);

if (myStream>>i)
success = true;
else
success = false;
Comment posted by 'Andres Jorge' on Oct 16 2007 @ 03:38:12
You saved my life, dude!
Comment posted by 'Anonymous' on Nov 18 2007 @ 23:25:28
I used it on linux gentoo and it works fine ! THANKS
Comment posted by 'a very happy man' on Nov 19 2007 @ 07:08:21
Worked like a charm, DPAK you a pimp!
Comment posted by 'BodiSafa' on Nov 29 2007 @ 05:28:12
This is nothing short of MONEY!!!!!!!!!! Thank you.
Comment posted by 'nassydocky' on Dec 29 2007 @ 02:17:12
YOu are Great,Fabulous. I really really owe you.
Comment posted by 'Dazza' on Jan 18 2008 @ 12:08:47
I don't understand why you say this only works on windows......
I'm using gcc on linux, and it works fine :)
Comment posted by 'briedis' on Feb 12 2008 @ 14:12:09
Thaaank youuuu!! Works great!
Comment posted by 'Anonymous' on Mar 21 2008 @ 04:57:50
gj work for me
Comment posted by 'sean' on Apr 20 2008 @ 18:50:51
nothing i say will be good enough to say how great you are. thank you so much!
Comment posted by 'TeacherDidtKnow' on Apr 23 2008 @ 15:43:29
thank you this is truly quality help.
Comment posted by 'Anonymous' on May 9 2008 @ 16:42:54
thanks .it really saved me
Comment posted by 'Anonymous' on Jul 12 2008 @ 16:49:07
thanks..
Comment posted by 'silk' on Aug 12 2008 @ 02:05:26
works just dandy on osx as well, nice one

Add a comment about this TechByte

If you wish to add a comment regarding this TechByte, please use the form below. Please note that by submitting comments using this form you are allowing all of the information submitted to be visible on this website. Any comments submitted using this form will only be shown on the website if they are approved by the administrators of this site. IF APPROVED, COMMENTS MAY TAKE SEVERAL DAYS TO BE POSTED.

Posted By: (Optional)

Comments:


Other TechBytes: