Hi guys!!
Happy Halloween, am still struggling with some C++ problems.
How can one Create a function whose input is an integer n which represents the number of days since the day you were born. The function should then return 3 integers corresponding to the month, day, and year of the date which occurs n days after you were born. (e.g. If you were born on 02=01=2011 and you enter n = 5 into the function, the function would return: 2 for the month, 6 for the day, and 2011 for the year). Assume that each month has 30 days and that a year always has 365 days.
so far i know how to calculate age, but how to solve this particular question. please help in editing my program
// Date of birth, find the age
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int Y; // The Year
int YY; // The Year (Maths)
int YYY; // The Year (Current)
int M; // The Month
int MM; // The Month (Maths)
int MMM; // The Month (Current)
int D; // The Day
int DD; // The Day (Maths)
int DDD; // The Day (Current)
// The Date.
cout << "This is a progran to tell how old you are..." << endl;
cout << "Please enter the current date. (In Numeral)" << endl;
cout << "" << endl;
cout << "" << endl;
cout << "Enter the current year: ";
cin >> YYY; // Enter current year of date and hold in YYY.
cout << "Enter the current month: ";
cin >> MMM; // Enter current month of date and hold in MMM.
cout << "Enter the current day: ";
cin >> DDD; // Enter current day of date and hold in DDD.
cout << "" << endl;
cout << "" << endl;
// The DOB.
cout << "Please enter your DOB." << endl;
cout << "Year: ";
cin >> YY; // Enter year of birth and hold in YY.
cout << "Month: ";
cin >> MM; // Enter month of birth and hold in MM.
cout << "Day: ";
cin >> DD; // Enter day of birth and hold in DD.
Y = (YYY - YY); // Year(Y) = Date(YYY) - Birth(YY).
M = (MMM - MM); // Month(M) = Date(MMM) - Birth(MM).
D = (DDD - DD); // Day(D) = Date(DDD) - Birth(DD).
cout << "" << endl;
cout << "You are " << Y << "/" << M << "/" << D << " years old." << endl; // Result.
system ("PAUSE");
return 0;
}
Okay thanks, so my question is
How can one Create a function whose input is an integer n which represents the number of days since the day you were born. The function should then return 3 integers corresponding to the month, day, and year of the date which occurs n days after you were born. (e.g. If you were born on 02=01=2011 and you enter n = 5 into the function, the function would return: 2 for the month, 6 for the day, and 2011 for the year). Assume that each month has 30 days and that a year always has 365 days.
// Date of birth, find the age
#include<iostream>
#include<cstdlib>
usingnamespace std;
int main()
{
int Y; // The Year
int YY; // The Year (Maths)
int YYY; // The Year (Current)
int M; // The Month
int MM; // The Month (Maths)
int MMM; // The Month (Current)
int D; // The Day
int DD; // The Day (Maths)
int DDD; // The Day (Current)
// The Date.
cout << "This is a progran to tell how old you are..." << endl;
cout << "Please enter the current date. (In Numeral)" << endl;
cout << "" << endl;
cout << "" << endl;
cout << "Enter the current year: ";
cin >> YYY; // Enter current year of date and hold in YYY.
cout << "Enter the current month: ";
cin >> MMM; // Enter current month of date and hold in MMM.
cout << "Enter the current day: ";
cin >> DDD; // Enter current day of date and hold in DDD.
cout << "" << endl;
cout << "" << endl;
// The DOB.
cout << "Please enter your DOB." << endl;
cout << "Year: ";
cin >> YY; // Enter year of birth and hold in YY.
cout << "Month: ";
cin >> MM; // Enter month of birth and hold in MM.
cout << "Day: ";
cin >> DD; // Enter day of birth and hold in DD.
Y = (YYY - YY); // Year(Y) = Date(YYY) - Birth(YY).
M = (MMM - MM); // Month(M) = Date(MMM) - Birth(MM).
D = (DDD - DD); // Day(D) = Date(DDD) - Birth(DD).
cout << "" << endl;
cout << "You are " << Y << "/" << M << "/" << D << " years old." << endl; // Result.
system ("PAUSE");
return 0;
}
So the main problem is the way you calculate the variables Y, M, D. It is way too simplistic.
What if the DOB was Oct 30 2011? Using today's date of 2 Nov 2012, gives the answer of 1 year, 1 month, negative 28 days.
It especially won't work when the current date is Feb say and DOB is Dec, which yields negative months.
The other thing I see is the poor choice of variable names IMO. You should try to use names which make their meaning obvious to ever is reading your code. Y & YY are too similar and confusing IMO.
There are several ways to do this properly.
The method hinted at in the question is the following:
Assign a Day Number to each date, starting from some arbitrary date such as 1 Jan 1900. In the simplified system mentioned in the question, 1 Jan 2000 would have a Day Number of 36500. Then subtract these, to arrive at the number of days between the dates. Then convert this into years, months days. This is easy in your case because of the simplified months and years, no leap years.
The problem says
Assume that each month has 30 days and that a year always has 365 days.
so we shouldn't worry about that.
How can i solve the problem then
Assume that each month has 30 days and that a year always has 365 days.
so we shouldn't worry about that.
On the contrary, that was a clue how to solve the problem. I explained it:
TheIdeasMan wrote:
Assign a Day Number to each date, starting from some arbitrary date such as 1 Jan 1900. In the simplified system mentioned in the question, 1 Jan 2000 would have a Day Number of 36000. Then subtract these, to arrive at the number of days between the dates. Then convert this into years, months days. This is easy in your case because of the simplified months and years, no leap years.
So have a go.
Actually this is a contradiction:
Assume that each month has 30 days and that a year always has 365 days.
Maybe they meant a year has 12 months and a month has 30 days.
Edit: I changed my reply because of the contradiction.