Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How can I convert a std::string to int?

Simply have a fast inquiry. I've checked out the web a lot and I've tracked down a couple of arrangements yet none of them have worked at this point. Taking a gander at changing a string over to an int and I don't mean ASCII codes.

For a fast run-down, we are passed in a condition as a string. We are to separate it, design it effectively and tackle the linear equations. Presently, in saying that, I'm not ready to change a string over to an int.

I realize that the string will be in either the form (- 5) or (25) and so on so it's absolutely an int. In any case, how would we remove that from a string?

One way I was believing is running a for/while loop through the string, check for a digit, separate every one of the digits after that and afterwards hope to check whether there was a main '- ', if there is, duplicate the int by - 1.

It appears to be somewhat over confused for a little issue, however. Any thoughts?
by

2 Answers

akshay1995
In C++11 there are some nice new convert functions from std::string to a number type.

So instead of

atoi( str.c_str() )

you can use

std::stoi( str )

where str is your number as std::string.
RoliMishra
use the atoi() function to convert the string to an integer:

string a = "25";

int b = atoi(a.c_str());

Login / Signup to Answer the Question.