Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to take a string with only numbers and convert it to an int...

Status
Not open for further replies.

danielnogo

Programmer
Joined
Jan 20, 2005
Messages
16
Location
US
Is there anyway to take a string with only numbers and covert it to an integer? I know you can do it vice versa.
 
I usually just use aoti:

int nNumber = 0;
CString string;
atoi(&string);
 
Yep just use atoi, there's also an atof to convert to a double if you need it.
 
Okay, so if I have a string that has just numbers inside of it, and I want to put it inside an int, i'd do this?:

int TheNumbers= 0;
atoi(m_sNums);

Then, how do I load it into the integer variable?
 
Code:
int theNumbers = atoi(m_sNums);


"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
If you are using CStrings, then it really needs a LPCTSTR operator i.e.
Code:
CString msrope;
msrope = "1234";
int x = atoi((LPCTSTR) msrope);
Similarly, if you are using STL strings
Code:
string stlrope;
stlrope = "1234";
int x = atoi (stlrope.c_str());
 
>If you are using CStrings, then it really needs a LPCTSTR operator i.e.

Which since it is a type operator will implicitly be invoked when needed. That's why a call like
Code:
CString msrope;
msrope = "1234";
int x = atoi(msrope);
works. (in non-unicode anyway)

>Similarly, if you are using STL strings

Which do not have such operators, hence you must call the c_str() method to get it as a const char*

/Per

www.perfnurt.se
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top