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?

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

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.

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.

>>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]);

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.

Member Avatar for iamthwee

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.