Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use atoi function with strings in C++

Tags:

c++

This is a basic question. I use C++ but not C++11. Now, I want to convert a string to an integer. I have declared like this:

string s;

int i = atoi(s);

However, this shows an error that such a conversion is not possible. I checked out the internet and I found that C++11 has stoi() but I want to use atoi itself. How can I do it? Thanks!

like image 754
John Yad Avatar asked Dec 24 '14 17:12

John Yad


1 Answers

Convert it into a C string and you're done

string s;

int i = atoi(s.c_str());
like image 170
Matteo Pacini Avatar answered Sep 25 '22 14:09

Matteo Pacini