Hi, fellow programmers.
I am programming about an age calculator.
It's simple:
1, get the input date 'today', check if its valid
2, get the input date 'birthday', check if it's valid and before 'today'
3, calculate the difference and output.
This is a assignment due tomorrow and I have written the most of it, just needs some debugging, mostly about pass by reference. I declared date1&date2 at main and I have no idea how to call the value of them if I want to calculate them at another function. I guess I should probably declare them at struct Date, but I am not sure.
I would really appreciate it if you guys could tell me what to do.
Code:
/************************************************************
*
* Project 4: How Old Are You Really?
*
* Author: xx
* Date: 6 May 2015
*
* This is a program designed to calculates the difference
* between two dates, which will be expressed in terms of
* years, months and days.
*
***********************************************************/
#include <bjarne/std_lib_facilities.h>
struct Date {
int month;
int day;
int year;
// Member functions
Date get_date();
Date get_birth_date();
bool is_valid_date(int year, int month, int day);
bool is_before(Date& date1, Date& date2);
Date calculate_age(Date& date1,Date& date2);
};
// Declaration
Date get_date();
Date get_birth_date();
bool is_valid_date(int year, int month, int day);
bool is_before(Date& date1, Date& date2);
Date calculate_age(Date& date1,Date& date2);
int main()
{
Date date1 = get_date();
Date date2 = get_birth_date();
Date get_date();
// Check if the date is correct
if (! is_valid_date(year, month, day))
error("Date is not valid.");
// Run how old or not?
char go_on;
cout << "Would you like to see how old you are (y/n)?\n";
cin >> go_on;
if (go_on=='n'){
cout << "You are so chicken!";
return 0;}
else if (go_on!='y')
error("Please enter y for yes or n for no.");
Date get_birth_date();
// Is birthday valid as well?
if (! is_valid_date(int year, int month, int day))
error("Date is not valid.");
// Is the birthday before 'today'?
if (!(is_before(Date& date1, Date& date2)))
error("Your birthday should not be later than today, terminator.");
// Calculate and give the answer.
Date calculate_age();
}
Date get_date()
{
Date date1;
cout << "Welcome to the age calculator!\n";
cout << "Please enter today's date (mm/dd/yyyy): ";
int m;
int d;
int y;
// To ignore the '/' during reading
char slash;
cin >> m;
date1.month = m;
cin >> slash;
cin >> d;
date1.day = d;
cin >> slash;
cin >> y;
date1.year = y;
cout << "Date entered was "<< date1.month << "/" << date1.day << "/" << date1.year <<"\n";
}
Date get_birth_date()
{
Date date2;
cout << "Please enter your birth date (mm/dd/yyyy): ";
int m;
int d;
int y;
// To ignore the '/' during reading
char slash;
cin >> m;
date2.month = m;
cin >> slash;
cin >> d;
date2.day = d;
cin >> slash;
cin >> y;
date2.year = y;
cout << "Your birthday is "<< date2.month << "/" << date2.day << "/" << date2.year <<"\n";
}
// Is date valid?
bool is_vaild_date(int year, int month, int day)
{
if (day<0)
return false;
if (month<1 || month>12)
return false;
// Check if the date exists or not considering there are have
// different days when month varies
int days_in_month=31;
switch (month){
case 2:
days_in_month=28;
break;
case 4: case 6: case 9: case 11:
days_in_month=30;
break;
}
if (days_in_month<day)
return false;
return true;
}
//Date check - is birthday before 'today'?
bool is_before(Date& date1, Date& date2)
{
if (Date& date1.year<Date& date2.year)
return false;
else if (Date& date1.month<Date& date2.month)
return false;
else if (Date& date1.day< Date& date2.day)
return false;
return true;
}
// Calculation
Date calculate_age(Date& date1,Date& date2)
{
Date date3;
date3.day = date1.day - date2.day;
date3.month = date1.month - date2.month;
date3.year = date1.year - date2.month;
if (date3.day<0){
date3.day = 30 + date3.day;
date3.month = date3.month - 1;
}
if (date3.month<0){
date3.month = 12 + date3.day;
date3.year = date3.year - 1;
}
cout <<"You are " << date3.year <<" years, "<< date3.month <<" months, and " << date3.day <<" days old.\n";
}