'Your age in months according to your date of birth
So for a school project I'm trying to make a program that calculates your age in months according to your date of birth. I'm writing this in C++. Please don't mind the use of unnecessary/extra code as I'm still learning. Please note: variable names and notes are in Dutch. When using for example 9/6/1996 as my birth date the program works fine, but when I use 9/6/1969 it returns 30, in other words it's not working and the problem seems to have something to do with the year. Any ideas on how to solve this? Thank you in advance. My code:
#include <iostream>
using namespace std;
int main() {
int huidigJaar = 2014;
int huidigeMaand = 8;
int huidigeDag = 19;
int geboorteJaar = 1;
int geboorteMaand = 1;
int geboorteDag = 1;
int totaalMaanden = 1;
cout << "Wat is je geboortejaar?";
cin >> geboorteJaar;
cout << "Wat is je geboortemaand?";
cin >> geboorteMaand;
cout << "Wat is je geboortedag?";
cin >> geboorteDag;
int verschilJaar = huidigJaar - geboorteJaar - 1; // niet iedereen is al jarig geweest
//kijken of het jaar erbij mag en totaal aantal maanden uitrekenen
if ( geboorteMaand < huidigeMaand ) { //de persoon is dit jaar al jarig geweest
verschilJaar = verschilJaar + 1;
if ( geboorteDag <= huidigeDag ) {
totaalMaanden = verschilJaar * 12 + ( huidigeMaand - geboorteMaand ); //de huidige maand komt erbij
}else {
totaalMaanden = verschilJaar * 12 + ( huidigeMaand - geboorteMaand ) - 1; //de huidige maand gaat eraf
}
}else if ( geboorteMaand == huidigeMaand ) {
if ( geboorteDag <= huidigeDag ) {
verschilJaar = verschilJaar + 1;
totaalMaanden = verschilJaar * 12; //heel jaar
}else {
totaalMaanden = verschilJaar * 12 + 11; //er komt niet een heel jaar bij maar wel 11 maanden, omdat de huidige maand niet meetelt
}
}else {
if ( geboorteDag <= huidigeDag ) {
totaalMaanden = verschilJaar * 12 + huidigeMaand + ( 12 - geboorteMaand ); //de persoon moet nog jarig worden, dus geen jaar erbij maar wel de maanden na zijn verjaardag
}else {
totaalMaanden = verschilJaar * 12 + huidigeMaand + ( 12 - geboorteMaand ) - 1; //zelfde maar dan een maand eraf omdat deze maand niet als een maand gerekend kan worden
}
}
return totaalMaanden;
}//main
Solution 1:[1]
I dont C++ or speak dutch, but i can run you through the psudo-code i'd use. From what i see, your code looks a lot more complex than it should be...unless its because of the dutch:
- enter the DOB as a string, then tokenize as a date object it using some C++ Date class,
- parse the month and year as an int,
- use the Date Class to get today's date, toknize it as well, or if the option is available, get the month & year alone
- get the difference between the year entered and the year provided and multiply by 12
- Subtract the month of birth from 12 to determine the age at the end of the year of birth.
- get the int of month of "today".
- by now, all your values would be in months. ADD them all together--that should be an answer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Don E |
