0

Can anyone help me?

#include <iostream> using namespace std; int main () { cout << "When were you born? Ex. 2002(year) 6(month) 4(day)\n"; int yearborn; int monthborn; int dayborn; cin >> yearborn; cin >> monthborn; cin >> dayborn; cout << "What is the date today?\n"; int currentyear; int currentmonth; int currentday; cin >> currentyear; cin >> currentmonth; cin >> currentday; int ageyear; int agemonth; int ageday; ageyear = currentyear - yearborn; agemonth = currentmonth - monthborn; ageday = currentday - dayborn; if (agemonth == -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -12){ agemonth = ; } //This will not work! ^^ cout << "You are " << ageyear << " years old with " << agemonth << " months and " << ageday << " days."; return 0; } So this is supposed to tell you how old you are in years, months, and days. There is only one problem if your birthday is bigger than the date the end result will be a bunch of negative numbers. Your age cannot be negative. I need to figure out a way to make a negative number positive. For example, if(agemonth == -5) {agemonth = 5} The only problem with that is that I will have to write it 12 times. It will take too long. I want something more along the lines of if (agemonth = a negative number) {make it positive} Or something like this if (agemonth == -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -12){ agemonth = ; ^^ That won't work I can't really explain why but I think you can figure it out easily. Any ideas?

12th Oct 2018, 5:14 AM
StormIncoming
5 Respostas
+ 12
[Continued]: Either you can set 30 days for all months or you can check the current month and set the no. of days the current month has, I am assuming 30 days for all months so, 1 month = 30 days add 30 days to the current date and subtract 1 month from the current month. So the current date will become like 2017/08/42 Now subtract two dates (C)2017/08/42 - (B)2000/09/20 If you notice, the current month is less than birth month now, so we have to carry 1 year (adding 12 months and subtracting 1 year as did above). So the date will be like : 2016/20/42 Now subtract two dates: (C)2016/20/42 - (B)2000/09/20 = 16/11/22 (16 yrs, 11 months, 22 days) This way you won't get any negative value. Hope it helps.
12th Oct 2018, 10:03 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 11
Well actually you need to carry 1 month if the birth date is greater than the current date. Similarly, you need to carry 1 year if the birth month is greater than the current month. Take an example where current date, month and year are greater than the birth date, month and year (C)2017/09/12 - (B)2000/02/05 = 17/5/7 (17 yrs, 5 months, 7 days) Now let's take an example where birth month is greater than the current month (C)2017/09/12 - (B)2000/10/12 In this case birth month is greater than current month so we will carry 1 year 1 yr = 12 months Add 12 months to current month and subtract 1 year from current year so now current date will be like 2016/22/12 Now subtract two dates: (C)2016/22/12 - (B)2000/10/12 = 16/12/0 (16 yrs, 12 months, 0 days) Now let's take an example where birth date is greater than current date (C)2017/09/12 - (B)2000/09/20 In the above date, we will carry 1 month, now here you can either set 30 days for all months or you can check the current month and set the no. of days
12th Oct 2018, 9:51 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 2
What was wrong with the answers you got here? https://www.sololearn.com/Discuss/1525823/?ref=app
12th Oct 2018, 6:54 AM
Anna
Anna - avatar
0
if(agemonth < 0) agemonth = -agemonth;
12th Oct 2018, 5:26 AM
fra
fra - avatar
12th Oct 2018, 6:03 AM
Daniele Bonomi
Daniele Bonomi - avatar