Monday, March 16, 2009

What's your Fractional Age?


I'm making a relatively [on the relativity of "light coding"] simple program that calculates a persons age with its fractional part (COPY PASTED from coderrach's Blog. [the text before this parenthetical]). I made this program to remind me how much time I have until I'm a year older to remind me that I shouldn't waste time on video games and other things that aren. I just finished! Holy cow, that took a while. I've been working on it for at least 4 hours, thinking of how to solve all the problems.

list of some problems I encountered [don't laugh]
  • Days left until your birthday devided by 365 is not the fractional part of your age; however it is the fraction of a year until your birthday.
  • Amount of days in each month is not 30 [I did not try to code for each month. I used 30.5 (close enough) Though, if you're curious, the average of days in a month out of all the months is 30.42 (rounded)]
  • When birth month was greater than current month, user was a year older.
  • When birth month was less than current month, months left until birthday was not birth month - current
  • "1 exit status. Permission Denied." Dev-C++ failed to let me know that my program was running therefore I was enable to compile. For 10 minutes, I wasted time changing my soure code thinking my code was bad. Arggg!
Here's the code for the program.

Code:

//caclulate your age in years plus its fractional part

#include <iostream>
using namespace std;

float DaysOfYearLeft (int, int, int, int);
int GetYears (int, int, int, int);


int main()
{
float age;
int bday; // birthday
int bmonth;
int byear;
int cday; // current day
int cmonth; // current month
int cyear; // current year
float fyear; // fractional year
float fday; // fractional day
float fage; // fractional age is the sum of fyear and fday


cout<<"This program calculutes your age plus the fractional part of your age.";
cout<<"\n\n Enter your numerical birth day, month, and complete year in that order each\n value seperated by a single space.\n";
cin>>bday; cin>>bmonth; cin>>byear;
cout<<"\n\n Enter the current numerical date day, month, and complete year in that order \neach value seperated by a single space.\n";
cin>>cday; cin>>cmonth; cin>>cyear;

fyear = GetYears (byear, cyear, bmonth, cmonth);
fday = 1 -(DaysOfYearLeft (bday, bmonth, cday, cmonth)/365) ;
fage = fyear + fday;

cout<< "\n\nYour age with its fractional part is "<< fage;
cout<< "\n That is " << GetYears(byear, cyear, bmonth, cmonth) << " years and "<< 365 - DaysOfYearLeft (bday, bmonth, cday, cmonth)<<" days.";
cout<< "\n" << DaysOfYearLeft (bday, bmonth, cday, cmonth) <<" days until your birthday!";


char f;
cin>>f;
return 0;
}

float DaysOfYearLeft (int bday, int bmonth, int cday, int cmonth){

float monthsleft;

if (bmonth > cmonth){
monthsleft = bmonth - cmonth; //months left until next ageplus1
}
else
if (bmonth < cmonth){
monthsleft = 12 - (cmonth - bmonth);
}
// function does not yet account for current month equal to birth month

float daysleft = (monthsleft * 30.5 + bday) - cday;

return daysleft;
}

int GetYears (int byear, int cyear, int bmonth, int cmonth){

int age;


if (bmonth > cmonth){
age= (cyear - byear) - 1;
}
else
if (bmonth < cmonth){
age = cyear - byear;
}

return age;
}
list of problems trying to get the HTML right on this post:

No comments: