By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,610 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,610 IT Pros & Developers. It's quick & easy.

how to convert string to integer??

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a


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.