a string isn't a function, its the data type in C++ which is used to represent string data (You might have been using char arrays up til now - but C++ strings are a far better way)
Don't use atoi - its unsafe, you can't reliably check for a valid conversion, which can lead to all sorts of problems.Quote:
it works just like i wanted it even displays + or - if i put it in. :)
but now if i wanna transform those digits in to numbers using atoi function i cant??
why wont it work.
or is there any other way to transform a std::string to integer
Code:#include <iostream>
#include <string>
using namespace std;
int main ()
{
string s5;
int a;
cout <<" iput string ";
cin>> s5;
string s3 (s5, 8, 3);
a = atoi (s3);
cout << "ns3: " << s3<<endl;
cout << " a = " << a <<endl;
return 0;
}
C++ has a facility called stringstreams which will do what you're after safely
Code:#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string str;
std::cout << "Enter a number: ";
std::getline(std::cin, str);
std::stringstream ss;
ss << str << 83;
int i;
if ( ss >> i )
std::cout << "Valid conversion to int: " << i;
else
std::cout << "Invalid input";
}