string to int

What am I doing wrong?

#include <string>
#include <iostream>
#include <stdlib.h>

using namespace std;

string strt0 = "1234567890";
string strt1 = str0.substr(4,2);
int strt2 = atoi(strt1);



error C2664: 'atoi' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
rgcommAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jkrConnect With a Mentor Commented:
Change

int strt2 = atoi(strt1);

to read

int strt2 = atoi(strt1.c_str());

and it should work...
0
 
jkrCommented:
To elaborate - the compiler is right to complain about a 'string' data type when 'char*' is expected. The STL 'string' doesn't havea 'by-default' conversion as e.g. the MFC 'CString' class has...
0
 
rgcommAuthor Commented:
Thank you very much!
0
All Courses

From novice to tech pro — start learning today.