This iterator is not iterating, whats wrong?

Problem is that the iterator is not iterating through the loop. I don't know why. #includes are in the header as is my wont.

#include "neutronFileReader.h"

using namespace std ;

neutronFileReader::neutronFileReader()
{
}

list<vector<float> > neutronFileReader::spectrum(char* filename)
{
    ifstream fin(filename) ;
    string binhi, binlo ;
    list<vector<float> > neutronSpectrum ;
    list<vector<float> >::iterator nS ;
    vector<float> EnergyProbability ;

    while(!fin.eof())
    {
        getline(fin, binlo, ' ') ;                        //get the binlo string
        cout << "binlo: "<<binlo << endl ;
        getline(fin, binhi, ' ') ;                        //get the binhi string
        cout<<"binhi: "<<binhi<<endl ;
        EnergyProbability.push_back(atof(binhi.c_str())+(atof(binhi.c_str()) - atof(binlo.c_str()))/2) ;    //store middle of bin as emission Energy
        getline(fin, binlo) ;                            //try not to waste memory space
        cout<<"prob: "<<binlo<<endl ;
        EnergyProbability.push_back(atof(binlo.c_str())) ;    //store emnission probability
        neutronSpectrum.push_back(EnergyProbability) ;    //put the vector in the list
        //cout<<neutronSpectrum<<endl ;
    }


    for(nS = neutronSpectrum.begin() ; nS != neutronSpectrum.end() ; nS++)  //go through the neutron spectrum
    {
        EnergyProbability = (*nS) ;
        cout << "binval: " << EnergyProbability[0] << " " << "binProb: " << EnergyProbability[1] << endl ;
        cout << "binval: " << (*nS)[0] << ", binprob: " << (*nS)[1] << ", memadd: " << &nS << endl ;          // print energy & prob to screen
    }

    return neutronSpectrum ;
} 

anyway, some help here would be greatly appreciated, have moved it into a while loop, yes this is all bug testing but it's a fairly important bit of code. Cheers guys, always learning.

Answers


You're not clearing out EnergyProbability between input loops. (*ns)[0] is therefore seeing the redundantly stored values from the first input, ignoring the new values which are actually in [2], then [4] etc.. Just add EnergyProbability.clear() before reading more values into it.


Are you sure you are populating the neutronSpectrum array, so it's not empty? Please make sure by adding this to the end:

if (neutronSpectrum.empty())
  cerr << "error: empty neutronSpectrum" << endl;

Maybe there is a problem with your input file (it's empty or unreadable), so you end up adding nothing to neutronSpectrum in the first place. Just to make sure, please add some cout statements to your while loop. Also it's worth checking for fin.error() after the while loop:

if (fin.error())
  cerr << "error: error reading input file: " filename << endl;

Need Your Help

Passenger/nginx not loading Sinatra app

nginx sinatra passenger rack

I'm trying to run a few Sinatra apps under sub-uri's, but it seems that Passenger isn't picking them up as Rack applications.

Clarification on separate sections of code inside one method with curly brackets

java performance design

Is that good practice to write code inside one method with separate sections of curly brackets?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.