Solved

string to int

Posted on 2000-04-04
3
233 Views
Last Modified: 2010-04-02
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 *'
0
Comment
Question by:rgcomm
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 2684938
Change

int strt2 = atoi(strt1);

to read

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

and it should work...
0
 
LVL 86

Expert Comment

by:jkr
ID: 2684945
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
 

Author Comment

by:rgcomm
ID: 2685002
Thank you very much!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now