3

I am trying to convert a string I read in from a file to an int value so I can store it in an integer variable. This is what my code looks like:

ifstream sin;  
sin.open("movie_output.txt");  
string line;  
getline(sin,line);  
myMovie.setYear(atoi(line));

Over here, setYear is a mutator in the Movie class (myMovie is an object of Movie class) that looks like this:

void Movie::setYear(unsigned int year)  
{  
    year_ = year;  
}

When I run the code, I get the following error:

error C2664: 'atoi' : cannot convert parameter 1 from 'std::string' to 'const char *'  
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
4
  • It's like one of those recurring newsgroup posts. We should just sync these with recurring answer posts and everyone will be happy. Commented Apr 14, 2010 at 2:54
  • ... and then add that same usual comment response for those who suggest using atoi(), and then seriously all will be swell. Commented Apr 14, 2010 at 2:57
  • @wilhelmtell: If you can find a duplicate, feel free to mark as such... Commented Apr 14, 2010 at 4:48
  • @Billy 1243428 2023519 1766150 893670 1883056 2393873 200090 1878001 1141741 1817992 504635 64782 2073054 225362 194465 1496536 2066184 and this isn't an exhaustive list, mind you. Commented Apr 14, 2010 at 5:54

5 Answers 5

10

Rather than using std::getline(std::string&, std::istream&), why not just use the stream extraction operator on the file?

ifstream sin;
sin.open("movie_output.txt");
unsigned int year = 0;
sin >> year;
myMovie.setYear(year);
Sign up to request clarification or add additional context in comments.

1 Comment

For error handling, when there was no number in the file, sin.fail() is true after the year extraction.
5

myMovie.setYear(atoi(line.c_str()));

4 Comments

ah, that works. thanks a ton. also, if you dont mind, could you please explain why the .c_str() is required?
@xbonez: Because atoi does not work on strings. It works on const char *s. atoi is a relic from the C standard. To make a string into a const char *, you call the c_str member.
C++ has 2 ways of storing strings - the first way is carried over from C, which is char*, basically an array (or pointer to an array) of chars, and the second way is the new std::string. However, they aren't equivalent and many functions expecting char*s (like the atoi function which was carried over from C) can't process std::string inputs. Thus in some cases you may need to get the C style string (c_str()) from the std::string in order to make it work.
this uses stdlib.h . is there a way to do it without it?
5
#include <boost/lexical_cast.hpp>

Use the lexical_cast:

int value = boost::lexical_cast<int>(line);

Comments

3

You can do atoi(line.c_str())

Another approach, using C++ streams, is:

stringstream ss(line);
unsigned int year;
ss >> year;

1 Comment

stream is definitely better option
1

The quick fix is to use line.c_str() which provides a const char* for atoi().

A better solution (if available) may be to use boost::lexical_cast(line). This is a neater version of the C++ism of pushing things into and out of a std::stringstream which has all the type conversions you are likely to need.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.