469,636 Members | 1,619 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

how to convert string to integer??

could you tell me how can i convet a string to integer??

#include <iostream>
#include <string>
using namespace std;

int main(){
int integer;
string buffer("123456789");

integer = buffer.substr(2, 4); // how to store the number as integer??

return 0;
}

Thanks a lot~
Jul 22 '05 #1
3 27013
news.hku.hk wrote:
int integer;
string buffer("123456789");

integer = buffer.substr(2, 4); // how to store the number as integer??


Save the substring into a string and then convert string into integer.
See the faq [38.2] How do I convert a std::string to a number?:

http://www.parashift.com/c++-faq-lit....html#faq-38.2
Jul 22 '05 #2
news.hku.hk schrieb:
could you tell me how can i convet a string to integer??


stdlib:
int atoi ( const char * string );
Convert string to integer.
Parses string interpreting its content as a number and returns an int
value.


#include<iostream>
#include<string>

using namespace std;

int main () {
int integer;
string buffer = "123456789";
cout << "String:" << buffer << endl;
integer=atoi(buffer.c_str());
cout << "Integer:" << integer << endl;
return 0;
}
Jul 22 '05 #3


marbac wrote:
news.hku.hk schrieb:
could you tell me how can i convet a string to integer??


stdlib:
int atoi ( const char * string );


long atol(const char *nptr);
double atof(const char *nptr);

- Use these two functions in case you are looking for a long or a
double (instead of an int).

HTH

--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by winthux | last post: by
16 posts views Thread by akarui.tomodachi | last post: by
5 posts views Thread by Allerdyce.John | last post: by
43 posts views Thread by Steven T. Hatton | last post: by
3 posts views Thread by tanishka singh | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.