nested if - then

I'm trying to program the following: create a starting cohort of 1000 age-1 individuals of random lengths simulated from a uniform random on [650, 750]. Then, 1)determine the number that mature at age-1 according to a logistic function (pMat)and place these lengths in an outfile. Place the complement of the 1000 age-1 individuals into a second outfile Iimmatures). 3) determine the number of the immatures that survive to age-2, given a mean survival rate (= 0.75), and place these individuals in a third outfile. The random numbers used are also placed in separate outfiles to check that they are simulalated correctly.
the code runs correctly to the extent of creating the age-1 matures and immatures.
BUT the numbers surviving are not correct. The number of survivors ("Age2) is (wrongly) identical to the number of non-surviving age-1 immatures ("Morts1") and there are duplicate lengths among them (each length should be distinct). I am unsure what I need to do to correct this and obtain the correct coding.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #include <iostream>
#include <random>
#include <fstream>

using namespace std;

int main()
{
    int nreps = 1000;
    double c0 = -7.5;//intercept of logistic prob of maturation at length L
    double c1 = 0.01;//logistic slope
    double g0 = 0.97; //intercept of annual length growth increment
    double g1 = 1.08; //slope of annual growth increment
    double cvg = 0.05; //st deviation of normal distribution of annual growth increment. 
    double surv1 = 0.75; //mean probability of survival of cohort immatures to age2

    double L2 = 0;
    double L2B = 0;
    double Age2 = 0;
    double M1 = 0; // age 1 immature morts (non-survivors to age 2)

    ofstream fout1("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\Cohort.txt");//initial cohort at first age-at-maturity
    ofstream fout2("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\R1.txt");//unif randoms for probability of maturation
    ofstream fout3("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\R2.txt");// unif randoms for probabiliyt of survival of initial cohort immatures
    ofstream fout4("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\pMat.txt");// length-specific maturation probability
    ofstream fout5("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\Mat1.txt"); //Age1 Matures
    ofstream fout6("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\Immat1.txt");//Age1 Immatures
    ofstream fout7("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\Morts1.txt");//Age 1 immatures that do not survive to age 2.
    ofstream fout8("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\Age2.txt");//Age2 survivors, both maturing and immatures

    if (!fout1) {
        cerr << "Problem opening the file.\n";
        return 1;
    }

    uniform_real_distribution<double> d1(650.0, 750.0);// for simulating the length distribution of the initial cohort
    uniform_real_distribution<double> d2(0.0, 1.0);// for probability of maturation of initial cohort
    uniform_real_distribution<double> d3(0.0, 1.0);//for probability of cohort+immatures-to-age2 survival
    random_device rd{};
    mt19937 gen(rd());

    for (int i = 0; i < nreps; ++i) {
        double L1 = d1(gen);
        double R1 = d2(gen);
        double R2 = d3(gen);
        fout1 << L1 << endl;
        fout2 << R1 << endl;
        fout3 << R2 << endl;

        double pMat = (exp(c0 + c1 * L1) / (1 + exp(c0 + c1 * L1)));
        fout4 << pMat << endl;

        if (R1 < pMat) { //determine if individual at length L matures

            double Mat = L1; // number of individuals in the initial cohort of 1000 age 1 individual that mature at age 1
            
            fout5 << Mat << endl;// copy mature age 1 individualsto file
        }
        else {
            L2 = L1; // number of individuals in the initial cohort that do not mature and remain immature
            L2B = L1; // copy L2
            fout6 << L2 << endl; //copy immature age 1 individuals to file.
        }

            if (R2 < surv1) { //probability that an immature of age 1 survives to the next age (age 2)
                Age2 = L2B; //number of immature age 1 individuals that survive to age 2
            }

            else {
                M1 = L2B;
                fout7 << M1 << endl;
                fout8 << Age2 << endl;

            }

    }

}
Here's a tidied up version of your code which makes it clearer what's going on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <random>
#include <fstream>

using namespace std;

int main()
{
    int nreps = 1000;
    double c0 = -7.5;
    double c1 = 0.01;
    double g0 = 0.97;
    double g1 = 1.08;
    double cvg = 0.05;
    double surv1 = 0.75;
    
    double L2 = 0;
    double L2B = 0;
    double Age2 = 0;
    double M1 = 0;
    
    ofstream fout1("fout1.txt");
    ofstream fout2("fout2.txt");
    ofstream fout3("fout3.txt");
    ofstream fout4("fout4.txt");
    ofstream fout5("fout5.txt");
    ofstream fout6("fout6.txt");
    ofstream fout7("fout7.txt");
    ofstream fout8("fout8.txt");
    
    if (!fout1)
    {
        cerr << "Problem opening the file.\n";
        return 1;
    }
    
    uniform_real_distribution<double> d1(650.0, 750.0);
    uniform_real_distribution<double> d2(0.0, 1.0);
    uniform_real_distribution<double> d3(0.0, 1.0);
    
    random_device rd{};
    mt19937 gen(rd());
    
    for (int i = 0; i < nreps; ++i)
    {
        double L1 = d1(gen);
        double R1 = d2(gen);
        double R2 = d3(gen);
        fout1 << L1 << endl;
        fout2 << R1 << endl;
        fout3 << R2 << endl;
        
        double pMat = (exp(c0 + c1 * L1) / (1 + exp(c0 + c1 * L1)));
        fout4 << pMat << endl;
        
        if (R1 < pMat)
        {
            double Mat = L1;
            fout5 << Mat << endl;
        }
        else
        {
            L2 = L1;
            L2B = L1;
            fout6 << L2 << endl;
        }
        
        if (R2 < surv1)
        {
            Age2 = L2B;
        }
        else
        {
            M1 = L2B;
            fout7 << M1 << endl;
            fout8 << Age2 << endl;
        }
    }
    
    return 0;
}


You have described your problem in such a way that only someone familiar or bothered with you jargon will be of any help.

I have your files as a pile of numbers. Which of the 8 files and what's wrong with them?

I'd suggest you go back to your test message and set it out in so it is at least readable and orderly (numbered and separate) paragraphs.
PS You might make your code legible and maybe even self documenting too if you choose sensible/non-cryptic variable names. 'L2B' as an example means bugger all to most people.

I suspect all of this undisciplined and un-planned coding is the solution to your otherwise simple problem.
@OP
I guess that means tidying up your crap solved the problem.
Well done me!
Now for the thank you and green tick.
I created a version of your code that just prints a table of the key values. Here are the first few lines:
pMat    Len     R1      R2      Mature? Survives
0.378   700     0.206   0.305   Y       Y
0.406   712     0.27    0.757   Y       N
0.335   681     0.519   0.189   N       Y
0.305   668     0.642   0.711   N       Y
0.284   658     0.837   0.662   N       Y
0.419   717     0.346   0.545   Y       Y
0.371   697     0.173   0.42    Y       Y
0.362   693     0.505   0.594   N       Y
0.336   682     0.367   0.428   N       Y


Notice anything odd about the underlined output? This reflects a bug in your logic.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <random>
#include <fstream>

using namespace std;

int
main()
{
    int nreps = 1000;
    double c0 = -7.5;		//intercept of logistic prob of maturation at length L
    double c1 = 0.01;		//logistic slope
    double g0 = 0.97;		//intercept of annual length growth increment
    double g1 = 1.08;		//slope of annual growth increment
    double cvg = 0.05;		//st deviation of normal distribution of annual growth increment. 
    double surv1 = 0.75;	//mean probability of survival of cohort immatures to age2

    double L2 = 0;
    double L2B = 0;
    double Age2 = 0;
    double M1 = 0;		// age 1 immature morts (non-survivors to age 2)

    uniform_real_distribution < double >d1(650.0, 750.0);	// for simulating the length distribution of the initial cohort
    uniform_real_distribution < double >d2(0.0, 1.0);	// for probability of maturation of initial cohort
    uniform_real_distribution < double >d3(0.0, 1.0);	//for probability of cohort+immatures-to-age2 survival
    random_device rd {};
    mt19937 gen(rd());

    cout << "pMat\tLen\tR1\tR2\tMature?\tSurvives\n";
    for (int i = 0; i < nreps; ++i) {
	double L1 = d1(gen);
	double R1 = d2(gen);
	double R2 = d3(gen);

	double pMat = (exp(c0 + c1 * L1) / (1 + exp(c0 + c1 * L1)));
	bool mature = (R1 < pMat);
	bool survives = R2 < surv1;
	cout.precision(3);
	cout.width(5);
	cout << pMat << '\t'
	     << L1 << '\t'
	     << R1 << '\t'
	     << R2 << '\t'
	     << (mature ? 'Y' : 'N') << '\t'
	     << (survives ? 'Y' : 'N')<< '\n';
    }

}

Topic archived. No new replies allowed.