Program Error

Pages: 12
For some reason my program will print out the same thing regardless of the input:
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
/* NAMESPACES */
using std::cout; 
using std::cin;
using std::setprecision;
using std::endl;
/*  START OF FUNCTION PROTOTYPS*/

/* JULIAN DAY FUNCT */
double get_julian_day (double y1, double m1, double d1)
{
        if (m1 > 2) {
            y1 = y1;
            m1 = m1;
        }
        
            else if (m1 == 1 || m1 == 2) {
                y1--;
                m1+=12;
            }
    
        
    double a1 = floor (y1 / 100.0);
    double b1 = 2 - a1 + floor (a1 / 4);
    return floor (365.25 * (y1 + 4716)) + floor (30.6001 * ( m1 + 1)) + d1 + b1 - 1524.5;
}
/* DECIMAL DAY FUNCT */
double dec_time (double h, double m, double s)
{
    double a = ((h * 60 * 60) + (m * 60) + s);
    return (a / 86400);
}
/* END OF FUNCTION PROTOTYPS */
//             SPACE
/* STARTING MAIN */
int main () {
    
// FUNCT PROTO     
double dec, DEC;  // dec time
double jd, JD; // get julian day 

double M1, Y1, D1;

double MN1, S1, H1;
double Y2, M2;


double s1, mn1, h1;
double d1, y1, m1;

double HH, MM, SS;

double ajdn;

double dhm;

// SUBTRACT
double ajd;

// ACTUAL DAY 
double ad, AD;

double ham;
double decimal;

  /* SETTING TIME AS A VARIABLE */  
  
   /* NOTE: Program good from 1900 AD and up */
  
  
  time_t rawtime;
  struct tm * timeinfo;
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  M1 = timeinfo->tm_mon;
  Y1 = timeinfo->tm_year;
  D1 = timeinfo->tm_mday;
  MN1 = timeinfo->tm_min;
  S1 = timeinfo->tm_sec;
  H1 = timeinfo->tm_hour; 
  Y2 = Y2 + 1900;
  M2 = M2 + 1;
   
/*     END         OF           TIME        */

//               SPACE


/*     START       OF          UI          */
cout << "     ELAPSED TIME via JULIAN DAY    "<< endl;



cout << " START OF PROG..." << endl;
cout << endl;
cout << endl;

// YEAR
cout << "Year: " << endl;
cout << endl;
cin >> y1;

//  MONTH
cout << "Month: " << endl;
cout << endl;
cin >> m1;

// DAY
cout << "Day: " << endl;
cout << endl;
cin >> d1;

//HOUR 
cout << "Hour: " << endl;
cout << endl;
cin >> h1;

//MINUTE
cout << "Minute: " << endl;
cout << endl;
cin >> mn1;

//SECOND
cout << "Second: " << endl;
cout << endl;
cin >> s1;
/*          END    OF     UI       */ 

//               SPACE
/* START OF CALCULATIONS */

// TIME INTO DECIMAL
DEC = dec_time (h1, mn1, s1);
dec = dec_time (H1, MN1, S1);

AD = DEC + d1;
ad = dec + D1;


// JD CALCULATIONS 
JD = get_julian_day (AD, m1, y1);
jd = get_julian_day (ad, M2, Y2);

// ELAPSED TIME TO DAY
ajd = JD - jd;


dhm = modf (ajd , &decimal);
HH = dhm * 24;
MM = HH * 60;
SS = MM * 60;

/*      END   OF   CALCULATIONS      */

cout << "Days: " << dhm << " Hours: " << HH << " Minutes: " << MM << " Seconds: " << SS << endl;

cin.ignore();
cin.get();
return 0;
}

ELAPSED TIME via JULIAN DAY

 START OF PROG...

Year 

1999

Month:

12

Day:

31
Hour:

23
Minute:

59
Second:

59
Days: -0 Hours: -0 Minutes: -0 Seconds: -0
   



I don't know how that is possible.





Last edited on
The hell? You defiled my function!

You're passing the parameters to my function backwards. It's YMD, not DMY.
That's why Date should be a class and the constructor should validate parameters.

Murphy's law: whatever a programmer can screw up, they will.

Design by contract: http://en.wikipedia.org/wiki/Design_by_contract ... This is why it was invented.
Helios, your function was written wrong mathematically. You must of misunderstood Buffbill.
As so did I.
I obtained the e-book of the source and they gave examples, then I was dumbfounded as to why it didn't work. So I tested it until I got the right answer to the test answers in the book.

Sorry...

fixed
1
2
3
// JD CALCULATIONS 
JD = get_julian_day (y1, m1, AD);
jd = get_julian_day (Y1, M1, ad);


Is it possible to put my time var thing into a function prototype?



now I get the output,

Days: 0 Hours: 0 Minutes: 0 Seconds: 0



That's why Date should be a class and the constructor should validate parameters.


How would I do that?
Last edited on
I think not. I copied his algorithm to the letter. If he wrote it wrong, that's another story. And that doesn't explain why you mutilated my format and the if, which does exactly the same, only uglierly.

1
2
3
4
	if (m<=2.0){
		y--;
		m+=12.0;
	}
-niceness=
1
2
3
4
5
6
7
8
9
        if (m1 > 2) {
            y1 = y1;
            m1 = m1;
        }
        
            else if (m1 == 1 || m1 == 2) {
                y1--;
                m1+=12;
            }
he wrote it wrong


That's why Date should be a class and the constructor should validate parameters.


How would I do that?
Last edited on
PanGalactic wrote:
That's why Date should be a class and the constructor should validate parameters.


beginner at programming wrote:
How would I do that?


Roughly, something like this:

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
#include <stdexcept>
#include <cmath>

class Date
{
    const int year;
    const unsigned month;
    const double day;
public:
    explicit Date(int y, unsigned m, double d)
    : year(y), month(m), day(d)
    {
        if (((year < -4716) or (year > 9999))
            or ((month == 0) or (month > 12))
            or ((day < 1.0) or (day >= 32.0)))
        {
            throw std::domain_error("Invalid date.");
        }
    }
    
    int y() const {return year;}
    unsigned m() const {return month;}
    double d() const {return day;}
};

double computeJulianDay(const Date& date)
{
    int yr = date.y();
    unsigned mo = date.m();

    if (mo == 1 or mo == 2)
    {
        yr -= 1;
        mo += 12;
    }
    
        
    const int a = yr / 100;
    const int b = 2 - a + (a / 4);
    const double result = floor(365.25 * (yr + 4716))
        + floor(30.6001 * ( mo + 1))
        + date.d() + b - 1524.5;
    
    return result;
}
@helios Those two ifs do look similar but they would only be the exact same if m1 was always an int and it was never <= zero.... Which in this case is right but assuming can be deadly. You have to remember Design by Contract.
Wow I am going to learn more about classes right now.

if (((year < -4716) or (year > 9999))


or is a keyword???


BTW
1
2
3
4
5
6
7
8
 {
        if (((year < -4716) or (year > 9999))
            or ((month == 0) or (month > 12))
            or ((day < 1.0) or (day >= 32.0)))
        {
            throw std::domain_error("Invalid date.");
        }
    }


Thank you I never thought of putting an error for incorrect parameters
Last edited on
beginner at programming wrote:
or is a keyword???


Of course "or" is a keyword. So is "and" and "not".

:-)

http://en.wikibooks.org/wiki/C++_Programming/Operators/Logical_Operators
@PanGalactic

I get how it works now,

But how do I use this in my main function.

Do I use it the same way as I did before,


JD = computeJulianDay ( m, d, y);

Or am I not right and I don't use it like a function at all (I think I am right)


Last edited on
kevinkjt2000: All the more reason not to test with m==1 || m==2.

EDIT: Also, don't correct your elders.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
double jd = 0.0;
try {
    Date date(m,d,y);
    jd = computeJulianDay(date);
} catch (std::domain_error& ex) {
    std::cerr << ex.what() << std::endl;
    return 1;
}

// jd has a valid julian date.
...


Just make sure that you are using the right types when calling the Date constructor. I have changed your types so that we are using integral types as appropriate.
BTW, I left your error in the code to show what will happen.
ex.what() isn't a function (or is it???)
return 1; should be return 0; because 1 means something isnt right.
Date date (m,d,y) should be in the format of Date date (y,m,d) Because that is how you declared it....
it compiles and exits right away...

I don't know what is wrong with it.
Last edited on
http://www.cplusplus.com/reference/std/stdexcept/domain_error/

what() is a member function from std::exception. You should get "Invalid date." printed to stderr if the date is not specified correctly.

It should return 1 from the exception handler because there was a problem.

You are right, date(y,m,d) is the right format.

Try this:

1
2
3
4
5
6
7
int main()
{
    Date date(1999, 12U, 31.95);
    std::cout << std::setprecision(15) << computeJulianDay(date) << std::endl;
    return 0;
}

Last edited on
Okay... But Why wouldn't this work:



1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
    
double jd = 0.0;
try {
    Date date(2000,1U,1.5);
    jd = computeJulianDay(date);
} catch (std::domain_error& ex) {
    std::cerr << ex.what() << std::endl;
    std::cout << std::setprecision (15) << jd << std::endl;
    std::cin.get();
    return 1;
}
}


Because isn't this for:

1
2
3
4
5
6
7
8
9
{
        if (((year < -4716) or (year > 9999))
            or ((month == 0) or (month > 12))
            or ((day < 1.0) or (day >= 32.0)))
        {
            throw std::domain_error("Invalid date.");
        }
    }



if the program worked it should print

2451545.0



or am I just confused...
Last edited on
Try this instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    double jd = 0.0;
    try {
        // Get the Julian date.
        Date date(2000,1U,1.5);
        jd = computeJulianDay(date);
    } catch (std::domain_error& ex) {
        // Error... the date was poorly formed.  Print an error message and exit with an error code.
        std::cerr << ex.what() << std::endl;
        return 1;
    }
    // Success!!  Print the Julian date, wait for user input and exit with a success code.
    std::cout << std::setprecision (15) << jd << std::endl;
    std::cin.get();
    return 0;
}


Note that the exception handler is only reached when an exception is thrown -- which only happens when there is an error in the date.
How do I add to the class and the main function to allow for two dates to be calculated at the same time, So I can subtract them in my main program?
Last edited on
You mean like this?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    double jd1 = 0.0;
    double jd2 = 0.0;
    try {
        // Get the Julian dates.
        jd1 = computeJulianDay(Date(2000,1U,1.5));
        jd2 = computeJulianDay(Date(1957,12U,27.43));
    } catch (std::domain_error& ex) {
        // Error... one of the dates was poorly formed.  Print an error message and exit with an error code.
        std::cerr << ex.what() << std::endl;
        return 1;
    }
    // Success!!  Print the difference, wait for user input and exit with a success code.
    std::cout << "difference:" << std::setprecision (15) << jd2 - jd1 << std::endl;
    std::cin.get();
    return 0;
}

Last edited on
Pages: 12