Math and Variable question.

Pages: 12

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
#include <iostream>
#include <iomanip>
using namespace std;
double d,m,y,a,b;
double jd;
int main()
{
    cout << "d" << endl;
    cin >> d;
    cout << "m" << endl;
    cin >> m;
    cout << "y" << endl;
    cin >> y;
    cout << endl;
cout << setprecision (16);
if (m>2) {
    y=y;  
    m=m;
} else if ( m <= 2 ) {
    y -= 1;
    m += 12;
}
a = (y/100);
b = 2 - a + (a / 4);
jd = (365.25 * (y +4716)) + (30.6001 * (m+1)) + d + b - 1524.5; 
cout << "= " << jd << endl;
cin.get();
return 0;
}


does any body know why, when I use cin.get() function my program crashes,
this is a reoccurring problem in all of my programs so I have to revert back to the system function.
Last edited on
Hey, I was born a leap day!


Wait, are you serious?
@buffbill Can some one explain how to use this formula. I don't understand because when I put in a non decimal date I get a decimal jd. Also I don't understand how to subtract the two dates and get the years, days, hours, minutes, seconds. Also buffbill you told me almost nothing about this formula. I need to know a little bit more to know how to complete this program.
BTW I don't know how you got yesterdays date (2455217.17222) as 1000 years ago and a couple hours off. I would appreciate if you went into greater detail about your formula and what i am trying to do. I am confused because I researched my previous formula extensively, as where I can't even find this one online.

Wait, are you serious?
Yes.

when I use cin.get() function my program crashes
Huh? It crashes? As in, you get an error message from the operating system?

Here's the code touched up. I think buffbill made a mistake before.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>
#include <cmath>

double get_julian_day(double y,double m,double d){
	if (m<=2.0){
	    y--;
	    m+=12.0;
	}
	double a=floor(y/100.0);
	return (365.25*(y+4716.0))+(30.6001*(m+1))+d+(2.0-a+floor(a/4.0))-1524.5;
}

int main(){
	std::cout <<std::setprecision(16)<<get_julian_day(2010,1,10)<<std::endl;
	return 0;
}
Last edited on
thanks, It looks a lot better.
But the thing is I still don't know how to use this formula correctly for what I am doing.
And buffbill can only explain that to me since he has the source.
http://cplusplus.com/forum/general/18398/page2.html#msg95859



Also heilos, I have never gotten how to pause my program correctly...


A lot of source code examples I see, don't appear to pause the program. i.e Opens then closes immediately

Dumb question \/
When you use return 0; does that make it pause?

Huh? It crashes? As in, you get an error message from the operating system?

It doesn't show a error from the OS. It just doesn't pause the program, as i thought cin.get() does.



Last edited on
Well that's not really a crash. And no, returning in and of itself doesn't pause; it just "ends" the function. In this case, it's main so control is returned to the OS, which closes your console window among other things seeing as how your program is finished.

If cin.get() isn't pausing, it's possible that there is other input left over in the buffer that cin is reading at that point. You'll want to empty the buffer completely before asking for the final input so that stops being an issue.
Ok thank you Zuge.
And no, returning in and of itself doesn't pause; it just "ends" the function.
I knew that I just was afraid to say it.

But, (to me) there seems to be another reason for why this is happening.
again I think this is a dumb question, Can you give me a example of input still left in the buffer.


buffbill http://cplusplus.com/forum/general/18398/page2.html#msg95859
No disrespect, but giving me a formula then vaguely telling me how to use it isn't very helpful.
I googled alot and I still cant find a pdf for Astronomical algorithms 1999 Jean Meeus.
So maybe more information can help.
i.e Can i be linked to where you are seeing this formula.
and more info i.e from when to when is this formula good for, How do I use it for what I am trying to do.

ugh... I can't think tonight... sorry for the about 10 edits.
Last edited on
Can you give me a example of input still left in the buffer.


The user inputs more than you ask for:
line1
line2


You read one line. So
line2
is left. Then you get to the end, and do cin.get() to pause. This reads 'l' and the program exits immediately.
Helios I tried to modify it for user input to no success.
code:

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
#include <iostream>
#include <iomanip>
#include <cmath>

double get_julian_day(double y,double m,double d){
	if (m<=2.0){
	    y--;
	    m+=12.0;
	}
	double a=floor(y/100.0);
	return (365.25*(y+4716.0))+(30.6001*(m+1))+d+(2.0-a+floor(a/4.0))-1524.5;
}

int main(){
   int m,d,y;
    std::cout << "year:" << std::endl;
    std::cin >> y;
    
    std::cout << std::endl;
    
    std::cout << "month:" << std::endl;
    std::cin >> m; 
   
    std::cout << std::endl;
    
    std::cout << "day:" << std::endl;
    std::cin >> d;
    
    std::cout << std::endl; 
        
        
        
        
	std::cout <<std::setprecision(16)<<get_julian_day(y,m,d)<<std::endl;
	std::cin.get();
    return 0;
}



Can you even call user input to functions?
Last edited on
Works for me.
And yes, you can pass user input to functions. If that wasn't the case, programming wouldn't make much sense.
Last edited on
Helios, after you typed in the date did it display the answer?
That is what's wrong.
For me once I finish typing in the day, It closes and disregards cin.get()

If cin.get() isn't pausing, it's possible that there is other input left over in the buffer that cin is reading at that point. You'll want to empty the buffer completely before asking for the final input so that stops being an issue.

If so, how so I empty the buffer?
And yes, you can pass user input to functions. If that wasn't the case, programming wouldn't make much sense.

Yeah I knew it would (should) work. But I wasn't sure because it is not working for me.
Last edited on
Helios, after you typed in the date did it display the answer?
Yes.
Then there is no reason for the code not to work.

Edit: What compiler are you using? Or would that have nothing to do with my issue?
Debugger: Breakpoints invalid...
What?
Last edited on
Oh, no, it doesn't pause at all when it finishes, but it works. Have you tried running it from the command line?
um?? How do I do that? Open it in a shell?


system ("pause"); works but cin.get(); doesn't
nor does getchar();
Last edited on
closed account (jy8CpfjN)
My Question is did the program B.A.P develop work with preciseness since his figures was imperfect, being possible for it to come in whole on leap day, but not possible any time else with out compensation for the leap year even at the 2 before that year which splits into the final six hour and go into beginning six hour of two different days. Helios that is exquisiteness in ur programming ability beside word problem solving ability how long u been programming.
Last edited on
@gratstone What the heck are you saying?


My Question is did the program B.A.P develop work with preciseness since his figures was imperfect, being possible for it to come in whole on leap day but not possible any time else with out compensation for the leap year even at the 2 before that year which splits into the final six and go into beginning six of two different days. Helios that is exquisiteness in ur programming ability beside word solving ability how long u been programming.


What?? Can you speak in simpler terms?


@helios, If the problem is the buffer, Then std::cout<< std::endl;
should empty it and cin.get() should work right?
No, it won't.
This subject has already been discussed to death on this forum. Personally, I don't know why someone would subject themselves to this torture when it's far easier to just run the program from a fucking console.
Topic archived. No new replies allowed.
Pages: 12