0

I'm using visual studio for the program. When i prompt the user for date like is this format -dd/mm/yyyy . For the program how do i use the data for just the year only not the whole input?

4
Contributors
6
Replies
7
Views
10 Years
Discussion Span
Last Post by iamthwee
0

That depends - Do you have an example you can post of the code where you're reading input from the user?

0

cin>>"Enter a Date(1)">>endl;


then the user enter 7/03/2007. But i need the year which the user input for other purpose like checking leap year.

0

cin>>"Enter a Date(1)">>endl;


then the user enter 7/03/2007. But i need the year which the user input for other purpose like checking leap year.

That doesn't help. Please post the actual code, and not a mock-up.

0

>>how do i use the data for just the year only not the whole input?

convert the last four characters of the string to an integer. There are several ways to do that but the simplest is to call atol()

std::string date = "07/05/2007";
int year = atol(&data.c_str()[6]);
0

this was what i'm asked to do to get this output
Enter Date[1]:3/7/1999
Date[1]: 3/7/1999
Date[1] is not in a leap year.

0

There are many ways to do this. The first that springs to mind is to use the "/" as a delimiter to split the string into parts (tokens).

All you really need to do then is get the last part and convert it to an integer, which should really be done using stringstreams as opposed to atol or any of its other crappy derivatives.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.