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