简体   繁体   English

输入后代码停止运行。 控制台给出消息:“(退出值:-1)”

[英]Code stops running after input. Console gives message: "(exit value: -1)"

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.我正在编写一个执行泊松过程的代码,该代码具有三个输入 lambda、开始日期和结束日期。

    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然后代码继续运行,但程序在最后一个输入处停止运行cin >> tsdate

I'm still pretty new to programming, but I still fell like this code should work, and not stop at: ```cin >> tsdate``我对编程还是很陌生,但我仍然觉得这段代码应该可以工作,而不是停留在:```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)".我正在使用 eclipse 并且当我停止时控制台会说以下内容:“(退出值:-1)”。

Normally it says " (exit value: 0)".通常它说“(退出值:0)”。 Maybe thats helpful for solving the problem.也许这有助于解决问题。

Thank you in advance.先感谢您。

You have wrongly named function dayinmonth in given code. 您在给定的代码中错误地命名了 function dayinmonth Or i can say you have not defined function dayinmonth . 或者我可以说你还没有定义 function dayinmonth
Or change name of function dayinyear to dayinmonth . 或者将 function dayinyear的名称更改为dayinmonth

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.我将 do-while 循环更改为 for-loop 然后它起作用了,我不知道为什么 do-loop 不起作用。 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.它几乎相同,但没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM