簡體   English   中英

輸入后代碼停止運行。 控制台給出消息:“(退出值:-1)”

[英]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)”。 也許這有助於解決問題。

先感謝您。

您在給定的代碼中錯誤地命名了 function dayinmonth 或者我可以說你還沒有定義 function dayinmonth
或者將 function dayinyear的名稱更改為dayinmonth

我不小心讓代碼工作了。 我將 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM