how to convert string to integer??

N

news.hku.hk

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~
 
M

marbac

news.hku.hk said:
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;
}
 
R

Rakesh Kumar

marbac said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top