Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3678400
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:20:12+00:00 2026-05-19T03:20:12+00:00

I have the following data in a c++ string John Doe 01.01.1970 I need

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:20:13+00:00Added an answer on May 19, 2026 at 3:20 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following xml string: <report> <item id=4219 Co=6063 LastName=Doe FirstName=John/> <item id=2571
I have the following data structures class Car { int CarID; string CarModel; string
I have the following connection string: Data Source=localhost\\SQLEXPRESS; , it complains that '' is
Suppose I have the following types of data: class Customer { String id; //
I have the following serialization method: Private Function SerializeData(ByVal data As cData) As String
I have a string of data with the following format: xpos-ypos-zoom (i.e. 8743-12083-15) that
I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
I have following data (String): Course1: A1 Course1: A2 Course2: B1 Course2: B2 Course2:
I have the following data that basically I only need a few bits of
let's said I have the following data: ID Key String Number 1 Name A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.