Public Group

# Quick Question

This topic is 2811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I need to get the first two digits of a four digit number such as a year. So say the number is 2010, I would need it to return 20. How would I go about doing this in C++?

##### Share on other sites
Did you try dividing by 100?

##### Share on other sites
...That was too easy. I may be back for some more help. This calculating days thing is a pain.

##### Share on other sites
Ok what about if I want to get the last two digits? So for 2010, I'd get 10.

##### Share on other sites
Did you try the modulus operator (%)?

##### Share on other sites
Will I tried doing %10 and %100 along with %4 but all I got was the four digit number back.

EDIT: nvm I got it to work. There was a syntax error that was messing me up.

##### Share on other sites
OK I managed to get it to tell me whether the year input is a leap year or not, but now I need to know how to get the month. I've already done this much:
#include <iostream>using namespace std;int getYearValue(int year);int getCenturyValue(int year);int getMonthValue (int month, int year);void getInput (int& month, int& day, int& year);int century (int year);int Year (int year);int main(void){ int month, day, year, yy; int getMonthValue = (month, year); cout << "Input a date" << endl; cin >> month >> day >> year;  int century = year/100; int Year = year%100;int getCenturyValue = 2*(3 - (century%4));int getYearValue = Year/4;int remainder = (getCenturyValue + getYearValue);bool isLeapYear(int year);{ if (year%400 == 0 || year%4 == 0)   {    cout << " is a leap year \n";   }else    {    cout << " is not a leap year \n";   }}

NOTE: Some things in there have yet to be done, I'm just trying to find the best way of doing this.

##### Share on other sites
The easiest way is just to just use a localtime struct and not have to compute any of this.

http://www.cplusplus.com/reference/clibrary/ctime/localtime/

If you have to do this for your homework then you're probably on your own.

Also your leap year formula is wrong.
Quote:
 Wikipedia: Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years.

EDIT: Actually I have no idea what you're trying to do with all that year=year%100 formulas, but in any case any year that is divisible by 4 is also divisible by 400, so it's redundant at best.

##### Share on other sites
Ok I have a large majority of it done. All I need help with now is to get the correct number for the day of the week. I need to do
(day + getMonthValue + getCenturyValue + getYearValue)/7
but what I need is to isolate the remainder. So if all that equaled 4 + 6 + 10 + 6 = 26. 26/7 = 3 remainder 5. The problem is I don't know how to get that remainder for my day.

##### Share on other sites
Again, the modulus operator(%) is used to calculate a remainder.

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999977
×