first of all, i'm new in programming. This is just a simple fun project for me, but i met unexpectedly problem and i don't know why.
Background: This is something like "Groundhog Day", start loop from a month, by next loop the time decrease by one second. I'm trying to calculate the total years (by counting the total seconds). Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int loop=1;
double secondsInDay = 60*60*24; //86400
double secondsInMonth = 86400*30; //2592000
float secondsInLoop = 86400*30;
double totalYears = 0;
long totalSeconds = 0;
cout <<"Loop : "<<loop<<endl;
totalSeconds += secondsInLoop;
cout <<"-> Total seconds: "<<totalSeconds<<endl;
cout <<fixed<<setprecision(0)<<"-->Total time in this Loop: "<<secondsInLoop;
cout <<fixed<<setprecision(3)<<" seconds / "<<secondsInLoop/60<<" minutes / "<<secondsInLoop/3600<<" hours / "<<secondsInLoop/86400<<" days"<<endl<<endl;
while (secondsInLoop>0)
{
if (loop%1000==0)
{
cout <<"Loop: "<<loop<<endl;
cout <<"-> Total Seconds: "<<totalSeconds<<endl;
cout <<fixed<<setprecision(0)<<"-->Total time in this Loop: "<<secondsInLoop;
cout <<fixed<<setprecision(3)<<" seconds / "<<secondsInLoop/60<<" minutes / "<<secondsInLoop/3600<<" hours / "<<secondsInLoop/86400<<" days"<<endl;
totalYears = totalSeconds/(secondsInMonth*12); //31104000
cout <<fixed<<setprecision(3)<<"-->> Total Years: "<<totalYears<<endl<<endl;
}
totalSeconds = totalSeconds+secondsInLoop;
secondsInLoop--;
loop++;
}
cout <<"Loop: "<<loop<<endl;
cout <<"#-> Total Seconds: "<<totalSeconds<<endl;
totalYears = totalSeconds/(secondsInMonth*12); //31104000
cout <<"#->> Total Years: "<<totalYears<<endl<<endl;
return 0;
}
Problem: In my code, in about loop 2461000, the Total Seconds is stuck and doesn't increase anymore. It's weird because the stuck number is 3359234850816, and it's far from the max number of long type. And, if i try to calculate normally (in another simple arithmetic code) there is no stuck like that.
Can anyone help please? Also, i'm open to any suggestion for tidy it up, or anything for the matter. Thanks