0

I'm new in programming and new in here. Sorry for stupid question but i have problem with result in my "calculate your age in seconds" code. It gives me weird result like 6.17725e+10 or -6.17414e+10. Program isn't finished yet but everything except results looks fine (i don't get any error. Sorry again and I hope for your understanding:)

#include <iostream>
using namespace std;



void title()
{
    cout << "Age Calculator" << endl << endl;

}

int byear()
{
    cout << "Enter your birth year: ";
    int by;
    cin >> by;
    return by;
}

int bmonth()
{
    cout << "Enter your birth month: ";
    int bm;
    cin >> bm;
    return bm;
}

int bday()
{
    cout << "Enter your birth day: ";
    int bd;
    cin >> bd;
    return bd;
}

int cyear()
{
    int cy;
    cout << "Enter current year ";
    cin >> cy;
    return cy;
}

int cmonth()
{
    cout << "Enter current month: ";
    int cm;
    cin >> cm;
    return cm;

}

int cday()
{
    cout << "Enter current day: ";
    int cd;
    cin >> cd;
    return cd;
}

void calculate(int by, int bm, int bd, int cy)
{
    double y = 31104000;
    long double cby = y * by;
    long double cbm = 259200 * bm;
    long double cbd = 8640 * bd;
    long double ccy = 31104000 * cy;

    cout << endl << cby << endl;
    cout << endl << ccy << endl;
    cout << endl << ccy - cby << endl;
}

int main()
{
    title();

    int by = byear();
    int bm = bmonth();
    int bd = bday();
    int cy = cyear();
    int cm = cmonth();
    int cd = cday();
    calculate(by, bm, bd, cy);
    cin.get();

    return 0;

}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Add your actual input too please. – πάντα ῥεῖ Mar 30 '16 at 18:37
  • For example: for 2010 as birth year it gives me 6.2519e+10 as a result. – 18-1-6-1-20 Mar 30 '16 at 18:40
  • 4
    `31104000` seconds is `360` days, `259200` is `3` days, `8640` is `144` minutes. I'm wondering what calendar do you use... – axiac Mar 30 '16 at 18:41
  • 2
    Is the question how do you read big numbers in scientific notation? – Kenny Ostrom Mar 30 '16 at 18:41
  • 1
    And what do you expect 2010 * 31104000 to yield ? – FredK Mar 30 '16 at 18:41
  • 1
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Mar 30 '16 at 18:42
  • 1
    Learning how to use a debugger is a useful advice, indeed. However, in this particular case, the problem is in the constants used in the code. Use `365*24*60*60` as the number of seconds in a year, `30*24*60*60` as the number of seconds in a month, `24*60*60` as the number of seconds in a day. Don't compute the values, let the compiler do it. It will perform the multiplications and put the correct values instead. Hopefully this is just an exercise and these approximations are good enough. But if you need to do exact calculations with dates and times, the standard library provides better ways. – axiac Mar 30 '16 at 18:47
  • Thank you for answers, it looks like i need to check everything again from beginning. – 18-1-6-1-20 Mar 30 '16 at 18:47
  • You might also want to think about how to combine all your input functions into one single function since they all do the exact same thing (read an int) except for the prompt. So if you pass the prompt to a generic function with the prototype `int prompt_for_int(const string &prompt_string)`, you could use one function to read all the inputs. – indiv Mar 30 '16 at 18:52
  • 1
    What happened to those leap years? – Ed Heal Mar 30 '16 at 18:53

3 Answers3

0

Like Kenny Ostrom commented, the shown values may look strange due to the scientific notation used by cout. To show all digits, you can change cout's precision using cout.precision(your_precision_here). See question below.

How do I print a double value with full precision using cout?

Community
  • 1
  • 1
Lauro Moura
  • 750
  • 5
  • 15
0

First, the numeric format you are confused by is "scientific notation". That will be enough info to open up a world of google searches, or you can just force it not to print in scientific notation.

Second, you really want to use a time library for any calendar stuff. It will handle all kinds of calendar weirdness for you, including leap years. Fortunately we have time.h

Third, I recommend using an integer type for seconds, partly to avoid rounding errors and ugly decimals, but mainly because that's what time.h uses. Just make sure it is big enough. My compiler uses a 64 bit integer for time_t, so I used that:

#include <time.h>
#include <memory>

time_t get_age_in_seconds(int year, int month, int day)
{
    struct tm birthday;
    memset(&birthday, 0, sizeof(birthday));
    birthday.tm_year = year - 1900;      // years since 1900
    birthday.tm_mon = month - 1;         // months since January (0,11)
    birthday.tm_mday = day;              // day of the month (1,31)

    time_t birthday_in_seconds = mktime(&birthday);
    time_t now = time(NULL);
    return now - birthday_in_seconds;
}
Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
0

Don't use doubles to do the calculation. You're not going to have any fractional values since you're not doing any division.

More importantly, look into mktime(), time(), and difftime(). You should be using these to do your calculation.

Rob K
  • 8,757
  • 2
  • 32
  • 36