morb
morb

Reputation: 17

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.

Upvotes: 1

Views: 263

Answers (4)

morb
morb

Reputation: 17

Ok, have checked all the above, one issue was that I was not clearing the EnergyProbability vector each iteration of the while loop (not that it was empty), this was creating a larger and larger vector which was pushed to the back of the list each time, so the first two elements of the pointed to list element (A vector) were being output, and these were the same each time (obviously, I am a fool). So, now I know the issue is in fact that the iterator is not recognising that it has reache the end of the list, and when it doees it is throwing a null pointer, I think. code here:

#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())
{
    EnergyProbability.clear() ;
    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
    cout << EnergyProbability[0] << ":" << EnergyProbability[1] << endl ;
    neutronSpectrum.push_back(EnergyProbability) ;  //put the vector in the list
}

for(nS = neutronSpectrum.begin() ; nS != neutronSpectrum.end() ; nS++)  //go through the neutron spectrum
{
    EnergyProbability = (*nS) ;
    cout << &neutronSpectrum.begin() << " : " << &nS << " : " << &neutronSpectrum.end() << endl ;         // print energy & prob to screen
}


return neutronSpectrum ;
}

output is:

0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8  
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 
0x28fbcc : 0x28fba8 : 0x28fbc8 

then code::blocks must realise it is in an infinite loop where nothing is changing and quits the code.

cheers guys

Upvotes: 0

Tony Delroy
Tony Delroy

Reputation: 106096

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.

Upvotes: 1

Andreas Brinck
Andreas Brinck

Reputation: 52519

If nothing gets printed in the for-loop at the end neutronSpectrum is probably empty.

Upvotes: 0

pts
pts

Reputation: 87271

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;

Upvotes: 1

Related Questions