Several errors in this code. Starting with include
#include <string.h>
#include <stdlib.h>
string
It doesn’t have the extension .h
. This is actually a bit confusing, in C++ the library headers do not have extension, but libraries in C do. When you see a library with .h
, She’s probably a C library, if she doesn’t have .h
, is from C++. As string
is a C++ library, remember, it has no extension.
i = atol(a);
Shouldn’t be atoi
? ASCII to integer
? Still this function will not work, atoi
is a function of C. Remember that string
is a C++ library? There are no C functions they receive string
, functions of C receive *char
, then you need to first convert this string
for *char
with the method a.c_str()
. Or else use stoi
, string to integer
.
Now just initialize your string, after all, if it doesn’t have a value, which you will convert to integer?
#include <string>
using namespace std;
int main () {
int i;
string a = "15";
i = stoi(a);
}
I see in the Ideone.
atoll receives a
const char*
as parameter. If you want to convert an Std::string to long int, usestol()
.– user142154
Part this, you need to initialize this string with some value.
– user142154