Began learning strings: Need to split one string into multiple

I am writing my first program using strings. In the program, I need to read in the date in a specific format and then do a whole bunch of stuff with that information.

However, I am stuck very early on. I need to write a function that will takes the 1 string containing the date and, split it into 3 separate strings: one for the month, one for the day, one for the year.

For example, if I was to enter 5/10/13, it would set month to 5, day to 10, and year to 13

I decided the easiest way to do this was to create 3 substrings out of it, but that wasn't yielding proper results. So, instead, I decided to try getting the first substring, then erasing that part of the string, getting another, erasing, etc.

Full program: http://pastebin.com/ezHwieuq

The function in question:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void breakoriginaldate (string origdate, string &month, string &day, string &year) 
{ 
int slash;
slash=origdate.find("/",0);
month=origdate.substr(0,slash);
cout<<month<<" is the month"<<endl;

origdate.erase(0, slash);
slash=origdate.find("/",0);
day=origdate.substr(0,slash);
cout<<day<<" is the day"<<endl;

origdate.erase(0, slash);
year=origdate;
cout<<year<<" is the year"<<endl;

return;
}


Getting the month out of it is simple, but I cannot figure out the proper way to get the day and year.
Last edited on
I changed the function to use origdate.erase(0, slash++); instead, but it is still yielding faulty results.

Here's the output:

1
2
3
4
5
6
7
Please enter the date in MM/DD/YY format, seperated by slashes.
For example, 6/11/08
1/23/88
1/23/88 is the original date
1 is the month
 is the day
/23/88 is the year
Alright, I think I solved it by changing it to slash+1 instead of slash ++
Try this. It is a little verbose, but is shows you how the find and substr functions can be used without erasing the original string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
	string date = "5/1/2012";

	int monthDelim = date.find("/", 0);
	int dateStart = monthDelim + 1;
	int dateDelim = date.find("/", dateStart);
	int yearStart = dateDelim + 1;

	cout << "Date: " << date << endl; 
	cout << "MonthDelim: " << monthDelim << endl;
	cout << "DateDelim: " << dateDelim << endl;

	cout << "Month: " << date.substr(0, monthDelim) << endl;
	cout << "Date: " << date.substr(dateStart, dateDelim - dateStart)
		<< endl;
	cout << "Year: " << date.substr(yearStart) << endl;

	return 0;
}
Last edited on
I'm kind of new to C++ as well, but I think I have an option for you. instead of passing the date into an INT, try passing it into a STRING. When a line is passed into a string it's actually being written into an array[]. then you just make a function with length() to determine where in the array to take your numbers. or run an if() test to find the slashes.

example;
string date = "05/01/12" //in the string date each number is placed in an array named date[]
string day, month, year;
int slashcount = 0
string end="no"
while(end=="no")
{
if(date[count]!="/" && slashcount==0)
{
day+=date[count]
count++
}
else if(slashcount==0)
{
slashcount++
count++
}
if(date[count]!="/" && slashcount==1)
{
month+=date[count]
count++
}
else if(slashcount==1)
{
slashcount++
count++
}
//... and do the same for year... then end the code.
//If you use length() to get the length of the date entered then you could be sure to end at the right time
}


This is not working code, just an example that I wrote here, there's probably an existing function in c++ to do what you want, but this was the idea I came up with. hope it helps. you should also look into the getline() function, I'm not entirely sure if that would do the same thing or better.



Last edited on
It could be easier to parse it using the I/O streams:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <sstream>
#include <iostream>
int main()
{
    std::string data = "5/1/2012";
    std::istringstream buf(data);
    int mon, day, year;
    char c1, c2;
    if( buf >> mon >> c1 >> day >> c2 >> year && c1=='/' && c2 == '/')
        std::cout << "month = " << mon << '\n'
                  << "day = " << day << '\n'
                  << "year = " << year << '\n';
    else
        std::cout << "Invalid date\n";
}


In general, there are many ways to tokenize a string in C++, especially if you can use boost, and using string::find() and string::substr() is probably the most laborous approach.
Topic archived. No new replies allowed.