Oct 9, 2015 at 1:27am UTC
I have an assignment to write a program that will calculate the amount of minutes that I have been alive given information inputted by the user, but for some reason all of my results have ended up as 0 so far (it has compiled successfully). I am not sure if the problem lies within the computations, or the struct that I have declared and I would greatly appreciate any assistance or hints that you might have.
My source code is below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#include <iostream>
#include <iomanip>
using namespace std;
struct birthType{
//birth time variables
int minuteOfBirth;
int hourOfBirth;
int dayOfBirth;
int monthOfBirth;
int yearOfBirth;
//current time variables
int currentMinute;
int currentHour;
int currentDay;
int currentMonth;
int currentYear;
};
//function prototypes
void getInformation(birthType Alex);
int calculateMinutes(birthType Alex);
void printMinutesAlive(int elapsedMinutes);
birthType Alex;
//constants
const int DAYS_IN_YEAR = 365;
const int MONTHS_IN_YEAR = 12;
const int AVERAGE_DAYS_IN_MONTH = 30;
const int HOURS_IN_DAY = 24;
const int MINUTES_IN_HOUR = 60;
//Return Variable
int elapsedMinutes;
int main(){
getInformation(Alex);
calculateMinutes(Alex);
printMinutesAlive(elapsedMinutes);
system("pause" );
return 0;
}
void getInformation(birthType Alex){ //This will gather information needed in calculations
cout << "Please input your Year of birth: " << endl << endl;
cin >> Alex.yearOfBirth;
cout << "Please input your Month of birth: " << endl << endl;
cin >> Alex.monthOfBirth;
cout << "Please input your Day of birth: " << endl << endl;
cin >> Alex.dayOfBirth;
cout << "Please input the hour of your birth (using 24 hr format): " << endl << endl;
cin >> Alex.hourOfBirth;
cout << "Please input the minute that you were born (out of the hour): " << endl << endl;
cin >> Alex.minuteOfBirth;
cout << "Please input the current Year: " << endl << endl;
cin >> Alex.currentYear;
cout << "Please input the current Month: " << endl << endl;
cin >> Alex.currentMonth;
cout << "Please input the current Day: " << endl << endl;
cin >> Alex.currentDay;
cout << "Please input the current hour (using 24 hr format): " << endl << endl;
cin >> Alex.currentHour;
cout << "Please input the current minute (out of the hour): " << endl << endl;
cin >> Alex.currentMinute;
}
int calculateMinutes(birthType Alex){ //This will calculate the minutes that I have spent alive.
int elapsedYears;
int elapsedMonths;
int elapsedDays;
int elapsedHours;
int elapsedMinutes;
elapsedYears = Alex.yearOfBirth - Alex.currentYear;
elapsedMonths = ((MONTHS_IN_YEAR - Alex.monthOfBirth) + (MONTHS_IN_YEAR * (elapsedYears - 1)) + Alex.currentMonth);
elapsedDays = ((elapsedMonths * AVERAGE_DAYS_IN_MONTH) + (AVERAGE_DAYS_IN_MONTH - Alex.dayOfBirth) + Alex.currentDay);
elapsedHours = (((elapsedDays * HOURS_IN_DAY) - HOURS_IN_DAY) + Alex.currentHour + (HOURS_IN_DAY - Alex.hourOfBirth));
elapsedMinutes = ((elapsedHours * MINUTES_IN_HOUR) + (MINUTES_IN_HOUR - Alex.minuteOfBirth) + Alex.currentMinute);
return elapsedMinutes;
}
void printMinutesAlive(int elapsedMinutes){
cout << endl << endl;
cout << "The amount of minutes that you have been alive is: " << elapsedMinutes << endl << endl;
}
Thanks for your time and assistance!
Last edited on Oct 9, 2015 at 1:28am UTC
Oct 9, 2015 at 1:56am UTC
getInformation() takes a parameter by value, so it doesn't initialize Alex.
The date difference algorithm is probably highly inexact, also. You should test it with pathological cases, such as diff(2015-01-01, 2014-12-31).