Solved

string to int

Posted on 2000-04-04
3
249 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Fully specialized class template function 21 154
C++ Language error 28 309
Create a path if not exists 7 123
Are the two lines the same? If not what is the difference? 2 30
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

737 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