I'm writing a code that does a poisson-proces, and the code has to three inputs lambda, the start date and the end date.
cout << "\nLambda: ";
cin >> lambda;
cout << "Start date 'dd.mm.yyyy': ";
cin >> t0date;
cout << "End date 'dd.mm.yyyy': ";
cin >> tsdate;
The code then goes on, but the program stops running at the last input cin >> tsdate
I'm still pretty new to programming, but I still fell like this code should work, and not stop at: ```cin >> tsdate``
This is the code I've written:
#include <iostream>
#include <cmath>
#include <random>
using namespace std;
const int nmax = 50;
int dayinmonth(string Dato);
void random_number(double &p);
int main() {
double p=0,dt[nmax],t[nmax],lambda;
int t0,ts,k=1, N;
string t0date,tsdate;
cout << "\nLambda: ";
cin >> lambda;
cout << "Start date 'dd.mm.yyyy': ";
cin >> t0date;
cout << "End date 'dd.mm.yyyy': ";
cin >> tsdate;
t0 = dayinmonth(t0date);
ts = dayinmonth(tsdate);
t[0] = t0;
do{
random_number(p);
dt[k] = (-1/lambda) * log(1-p);
t[k] = t[k-1] + dt[k];
k = k +1;
}while(t[k] < ts);
N = k -1;
cout << endl << "N: " << N << endl;
cout << "\nt_k: "
for(int i=0;i<N;i++){
cout << endl << t[k];
}
}
int dayinyear(string Dato){
string il;
int mth, daymth,day;
int Days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
mth = 0; daymth = 0; il = Dato;
mth = ((il[3] - '0') * 10 + (il[4] - '0')) -1;
daymth = (il[0] - '0') * 10 +(il[1] - '0');
day = 0;
for(int i=0; i<mth; i++) day += Days[i];
day += daymth;
return day;
}
void random_number(double &p) {
random_device rd;
mt19937 gen(rd());
uniform_real_distribution <double> dis(0.0, 1.0);
p = dis(gen);
}
I'm using eclipse and the console say the following when i stops: " (exit value: -1)".
Normally it says " (exit value: 0)". Maybe thats helpful for solving the problem.
Thank you in advance.
I accidentally got the code to work. I changed the do-while loop to a for-loop and then it worked, I have no idea why do-loop didn't work. If anyone knows why, please tell.
#include <iostream>
#include <cmath>
#include <random>
using namespace std;
const int nmax = 50;
int dayinyear(string Dato);
void random_number(double &p);
int main() {
double p=0,dt[nmax],t[nmax],lambda;
int t0,ts,k, N;
string t0date,tsdate;
cout << "\nLambda: ";
cin >> lambda;
cout << "Start date 'dd.mm.yyyy': ";
cin >> t0date;
cout << "End date 'dd.mm.yyyy': ";
cin >> tsdate;
t0 = dayinyear(t0date);
ts = dayinyear(tsdate);
t[0] = 0 ;
for( k=1; k<1000; k++){
random_number(p);
dt[k] = (-1/lambda) * log(1-p);
t[k] = t[k-1] + dt[k];
if(t[k]>ts) break;
}
N = k -1;
cout << endl << "N: " << N << endl;
cout << "\nt_k: ";
for(int i=0;i<=N;i++){
cout << endl << t[i];
}
}
int dayinyear(string Dato){
string il;
int mth, daymth,day;
int Days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
mth = 0; daymth = 0; il = Dato;
mth = ((il[3] - '0') * 10 + (il[4] - '0')) -1;
daymth = (il[0] - '0') * 10 +(il[1] - '0');
day = 0;
for(int i=0; i<mth; i++) day += Days[i];
day += daymth;
return day;
}
void random_number(double &p) {
random_device rd;
mt19937 gen(rd());
uniform_real_distribution <double> dis(0.0, 1.0);
p = dis(gen);
}
It's pretty much the same, but works without any problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.