5

I have the following data in a c++ string

John Doe 01.01.1970 

I need to extract the date and time from it into int variables. I tried it like this:

int last_space = text_string.find_last_of(' '); int day = int(text_string.substr(last_space + 1, 2)); 

But I got invalid cast from type ‘std::basic_string’ to type ‘int’. When I extract the "John Doe" part in another string variable, all works fine. What's wrong?

I am trying to compile it with g++ -Wall -Werror.

5 Answers 5

5

You need to use

std::stringstream ss; ss << stringVar; ss >> intVar; 

or

intVar = boost::lexical_cast<int>(stringVar);.

The later is a convenience wrapper from the boost library.

Sign up to request clarification or add additional context in comments.

5 Comments

@DevSolar: Of course, you're right. Will work the other way too. I edited the post.
@James: I'm very aware of atoi, but I would not use it in C++ code.
@James, Axel is not saying that at all. I very much doubt he'd be bothered by it unless it became apparent that a lexical_cast call was the cause of a performance issue in the application. I am certain your customer isn't going to notice.
@James: I prefer orthogonality and generality over using a potentially better performing C function. But last time I checked they were equally fast anyway. Yes, compilers are that smart nowadays.
@ James Goddard: A peephole timing is utterly us-less. It will make no difference in the greater context of a program that has all sorts of other stalls that affect timing in it. The ability to write code orthogonality and thus more maintainability is of much more importance in the long term.
3

Use streams to decode integers from a string:

#include <iostream> #include <sstream> #include <string> int main() { std::string x = "John Doe 02.01.1970"; std::string fname; std::string lname; int day; int month; int year; char sep; std::stringstream data(x); data >> fname >> lname >> day >> sep >> month >> sep >> year; std::cout << "Day(" << day << ") Month(" << month << ") Year(" << year << ")\n"; } 

The operator >> when used with a string variable will read a single (white) space separate word. When used with an integer variable will read an integer from the stream (discarding any proceeding (white) space).

Comments

2

Try the Boost Data/Time library.

Comments

0

As far as I can tell, atoi does what you need.

"Parses the C string str interpreting its content as an integral number, which is returned as an int value."

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

4 Comments

I get the following error: cannot convert ‘std::basic_string<char>’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’
@skazhy: You have to call the c_str() function of the string class when passing it to standard C functions. It returns you a const char*
in band signally of errors can be a problem. imho atoi is rarely useful because of this.
give me an example
0

Assuming (and that might be a bad assumption) that all the data was formatted similarly, I would do something like this

char name[_MAX_NAME_LENTGH], last[_MAX_NAME_LENGTH]; int month, day, year; sscanf( text_string, "%s %s %2d.%02d.%04d", first, last, &month, &day, &year ); 

This does however, have the problem that the first/last names that appear in your input are only one word (i.e. this wouldn't work for things like "John M. Doe"). You would also need to define some appropriate maximum length for the string.

It's hard to be more definitive about this solution unless we know more about the input.

2 Comments

sscanf(text,"%s", word); is an open invitation for buffer overruns. You really must check the size of word in text before calling sscanf, which makes it unsuited.
Don't use _ like that in your identifiers. Identifiers starting with _M are reserved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.