Manipulating inputs

Hi there, this is (I hope) an easy one for my first post. I've searched, but it's hard to find stuff when you're not 100% on what you're looking for.

I'm working through Lafore - C++ Object Oriented Programming, and one of the exercises asks you to take a date input with the format 10/9/2012 and put the day, month and year into separate int members of a struct.

I can't figure out how to do this. I need some way to get each integer and discard the slashes. Best,

T
Turn the date into a string (eg. 10/09/2012) and then turn the first and second character of the string into an int (as shown below) then repeat with the 3rd and 4th for the day and 6th -9th for the year. Hope I could help

1
2
string word = "10/09/2012";
	int day = (word[0] - '0') * 10 + (word[1] - '0');
Thanks, that makes sense!

Although I guess it would be a problem if I input a single digit day or month. I'll see if I can figure out an adaptive way.
This would also work however you would have to enter a space or enter in between each time frame (eg. 10 9 2012 instead of 10/9/2012) but it allows you to enter 9 instead of 09.

1
2
3
	string day, month, year;
	cin >> day >> month >> year;
	cout << day << "/" << month << "/" << year;


Hope it is more useful
Last edited on
Topic archived. No new replies allowed.