I'm new to C++, been programming a while in C though. Trying to read in a string and then convert the string into int by using strtol. Iam using the gcc compiler. And I get the following error message: "c++ error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'const char*' for argument '1' to 'long int strtol(const char*, char**, int)' int c = strtol(str[j], &p, 10);". I have tried different types of conversions but really like the strtol for future reference. Is there something to do with my vector string?
#include <iostream> #include <string> #include <cstdlib> using namespace std; using std::string; int main() { int a = 0; int i = 0; int size = 0; int* big; cin>>a; size = a*2; int sizes[size]; string *str = new string[size]; while(i < a){ cin>>str[i]; i++; } i = 0; while(i < a){ cout << str[i] << endl; // just for checking i++; } for (int j =0; j<size-1;j++){ char* p; char* q; int c = strtol(str[j], &p, 10); if (!*p) { sizes[j] = *p; }else{ sizes[j] = *p/2; } } return 0; } Thanks in advance!
int sizes[size];is not standard C++, this is an extension which means this code is not portable. Alsousing std::string;is rendundant when you also haveusing namespace std;. Also you never deletestring *str = new string[size];(which you should, although an ever better solution would bestd::vector)