Sign in to follow this  
Out-Take

Quick Question

Recommended Posts

Out-Take    100
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
Out-Take    100
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
Out-Take    100
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
karwosts    840
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
Out-Take    100
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this