Solved

string to int

Posted on 2000-04-04
3
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

691 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