[英]Code stops running after input. Console gives message: "(exit value: -1)"
我正在编写一个执行泊松过程的代码,该代码具有三个输入 lambda、开始日期和结束日期。
cout << "\nLambda: ";
cin >> lambda;
cout << "Start date 'dd.mm.yyyy': ";
cin >> t0date;
cout << "End date 'dd.mm.yyyy': ";
cin >> tsdate;
然后代码继续运行,但程序在最后一个输入处停止运行cin >> tsdate
我对编程还是很陌生,但我仍然觉得这段代码应该可以工作,而不是停留在:```cin >> tsdate`
这是我写的代码:
#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);
}
我正在使用 eclipse 并且当我停止时控制台会说以下内容:“(退出值:-1)”。
通常它说“(退出值:0)”。 也许这有助于解决问题。
先感谢您。
我不小心让代码工作了。 我将 do-while 循环更改为 for-loop 然后它起作用了,我不知道为什么 do-loop 不起作用。 如果有人知道为什么,请告诉。
#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);
}
它几乎相同,但没有任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.