Taking integers from a string

This is a discussion on Taking integers from a string within the C++ Programming forums, part of the General Programming Boards category; Hello, this is my first question on this site. I have a string with a date in it, like "17/02/1943", ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    2

    Taking integers from a string

    Hello, this is my first question on this site. I have a string with a date in it, like "17/02/1943", in the format dd/mm/yyyy. I need to get the date from this string, and put it into my Date struct, which has three int's for Day, Month and Year. How would I go about getting the numbers out, would I get substrings and cast them or something?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,675
    There are a couple different ways... The first involves using the atoi function:
    Code:
    #include <string>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
        string str("14/05/1988");
        int day, month, year;
    
        day = atoi( str.substr(0,2).c_str() );
        month = atoi( str.substr(3,2).c_str() );
        year = atoi( str.substr(6,4).c_str() );
    
        cout << "Day: " << day << " Month: " << month << " Year: " << year << endl;
    
        return 0;
    }
    Or you can use string streams:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int main()
    {
        string str("14/05/1988");
        int day, month, year;
        istringstream sstr(str);
        char dummy;
    
        sstr >> day >> dummy >> month >> dummy >> year;
    
        cout << "Day: " << day << " Month: " << month << " Year: " << year << endl;
    
        return 0;
    }
    Both ways should output the same thing:
    Code:
    Day: 14 Month: 5 Year: 1988
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    2
    Not too sure about that istringstream thing, and our lecturer looks down on anyone who uses C-style libraries or functions (i.e. cstdlib.h, cstring.h).

    I can get the substrings to just have the number I want ("32"), but can't convert them to int format. <static_cast><int> just gives me heaps of errors.

    Here's my code, Date is a struct:

    Code:
    #include <iostream>
    #include <string>
    #include "Date.h"
    
    using std::cout;
    using std::string;
    
    Date fileInputDate(string entry)
    {
    	Date output;
    	output.Day = static_cast<int>(entry.substr(0,2));
    	output.Month = static_cast<int>(entry.substr(3,2));
    	output.Year = static_cast<int>(entry.substr(6,4));
    	return output;
    }
    This gives me 3 C2440 errors. Is there anyway to do this without using C-style commands or any other libraries?

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    127
    >Is there anyway to do this without using C-style commands or any other libraries?
    You could always do the conversion manually. For small numbers like those in a date, you don't need to worry about the problematic cases that more sophisticated string-to-integer libraries must consider. The conversion is a simple loop.
    Code:
    Date fileInputDate(string entry)
    {
      Date output; // Assumes that output is zero initialized
    
      for (int i = 0; i < 2; i++) {
        output.Day = 10 * output.Day + (entry[i] - '0');
      }
      for (int i = 3; i < 5; i++) {
        output.Month = 10 * output.Month + (entry[i] - '0');
      }
      for (int i = 6; i < 10; i++) {
        output.Year = 10 * output.Year + (entry[i] - '0');
      }
    
      return output;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21