Question

I want to write a vector to a file, which represents the outcomes of a function that increments j with steps of "double Golf_dl". The vector itself must have "int Golf_NL" elements in it. (Golf_dl is a private member of class Tsunami) The problem is that I get a constant vector with all values equal to 0.5, which is rubbish. Can someone see what's wrong?

Code:

void T_Tsunami::Toestand(int nl)
{
    struct pb
    {
        std::vector<double>& v_;  

        pb(std::vector<double>& v) 
        : v_(v){}; 

        pb& operator+(double i) 
        {
            v_.push_back(i);
            return *this;  
        }
    };

    for( double i = 0; i < nl; i++ )
    {   
        double j=0; 
        double y = 0.25*(1-tanh(double(j-75)/5));
        j+=Golf_dl;
        pb(T_1)+y; 
    }
}

void T_Tsunami::Write(vector<double>d)
{
        const int Naamgrootte=64; 
        char Naam[Naamgrootte];
        std::cout << "Geef een bestandsnaam in" << std::endl;
        std::cin >> Naam;
        std::cout << std::endl;

        std::ofstream outFile(Naam);
        if (!outFile)
        {
            std::cerr << "Kan bestand niet openen" << std::endl;
            exit (1);
        }

        for (int i=0; i<100; i++)
        {
            outFile << d[i] << std::endl;
        }
        outFile.close();    
}
Was it helpful?

Solution

Your problem is in here:

for( double i = 0; i < nl; i++ )
{   
    double j=0; 
    double y = 0.25*(1-tanh(double(j-75)/5));
    j+=Golf_dl;
    pb(T_1)+y; 
}

Consider the fact that the loop variable, i, is not used anywhere inside the loop. Also, j and y do not retain their values between loops (they are not static).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top