Jump to content
  • Advertisement
Sign in to follow this  
Out-Take

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!