Else statement failing to execute in a four loop

I'm not sure where my error is coming from because it complies with no errors. The idea behind this code is that i have a set of functions that fill arrays and vectors and I want to use a for loop to run the function multiple times. I can loop it so it runs 2 times but anymore than that it just crashes. After removing each function and then adding in one at a time I found that the lines 24-25 (about incrementing i) are causing the code to crash.Here is the code

MAIN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <math.h>
#include <map>
#include <utility>
#include <vector>
#include <string>
#include "random_test3.h"
#include "random_test3_functions.cpp"

int main(int nchannel, double hitprob){
  int i=0;
   int g=0;
  srand( time(NULL) );
  
      CReadout (10,.5,i,g);
   return 0;
}


HEADER
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
#ifndef _CREADOUT_H
#define _CREADOUT_H
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <math.h>
#include <map>
#include <utility>
#include <vector>

using namespace std;

class CReadout {
    private:
        int buffer_ary[100];///////////////////////////////////////adjust buffer size
        vector <vector <int> > fifo_vec;
        map<int,int> fifo_map;
        vector<int> channel;
    public: 
         
         CReadout(int,double,int,int); 		  
    	~CReadout();
    	
    	int nchannel;
        double hitprob;
        
            void loadFifo_vec(int);            	
           // void UnassignRnd(int);
            void CFifo(int);
            void Buffer(int);
            void AssignRnd(int,double);
            int getChannel(int i){return channel[i];}
};
#endif 


FUNCTIONS
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
82
83
84
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <math.h>
#include <map>
#include <utility>
#include <vector>
#include <string>




using namespace std;


CReadout::CReadout( int nchannel, double hitprob, int i,int g ) {
	for (int m=0;m<2;m++){ //adjust the m value for more loops
    channel.resize(nchannel);
	AssignRnd( nchannel,hitprob );
	loadFifo_vec( i);
	Buffer( g);
    CFifo( i);
      if (i==10) i=0;
        else i++; 
    } 
}
CReadout::~CReadout( ) {
	
}

void CReadout::AssignRnd(int nchannel,double hitprob){ 
   
  double random;
  channel.resize(nchannel);
     for( int a=0; a<nchannel; a++) { 
	  random = rand() % 10000;
	  double value = random/10000;  
	
    if( value < hitprob ) channel[a]=1 ;
	else channel[a]=0;
   
	   }
}
  


void CReadout::loadFifo_vec(int i){
	
	fifo_vec.resize(channel.size() );
		for (int t=0;t<channel.size();t++) {
            if (fifo_vec[t][10]!=1){    //full statement and set size
        	    if ( getChannel(t)==1){
            	 	fifo_vec[t][i]=1;
                }
            }
                       
        }
}



void CReadout::Buffer(int g){
     
     for (int t=0;t<channel.size();t++)   {  
            for (int j=0;j<i;j++){ //adjust the j value for more loops
               if(fifo_vec[t][j]==1) { 
                   buffer_ary[g]=1;
                   g++;
               }
    }
     }
       cout<<g<<endl;		  
}



void CReadout::CFifo(int i){
    for(int t=0;t<channel.size();t++){
      if (fifo_vec[t][10]==1){   
        fifo_vec.clear();
    }
}
}
Last edited on
I don't know if this is your problem, but in CFifo(), when you have cleared fifo_vec, you should exit the for loop, because the next iteration will check position t of a vector that is empty
Good point, You know this part of my code has not been used so I dont think it is crashing but it is something i will have to fix. Do you would know how to clear a single vector that is inside a vector, because in CFifo if the the 'if' statement clears all the vectors not just the one where the 10th element is 1.
But as far as the original problem, i've been playing around and its definitely lines 24 and 25. The i doesnt seem to increment, i did something likeif(i==3) cout<<"hello"<<endl; and it never said hello, i never got passed 1. In fact if i set i to any other value other than 0 or 1 the program crashes.
Last edited on
Well, you only appear to be incrementing i in one place: line 25. And that increment depends on i having already been incremented elsewhere.
increment depends on i having already been incremented elsewhere.

Is that because i is in the else statement because I've tried it without an else statement and it still crashes, i.e just putting i++ in the for loop.
Last edited on
Can anyone help?
Last edited on
closed account (DSLq5Di1)
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
void CReadout::loadFifo_vec(int i){
	
	fifo_vec.resize(channel.size() );
		for (int t=0;t<channel.size();t++) {
            if (fifo_vec[t][10]!=1){ // You've resized fifo_vec, but the vectors inside your vector are empty..
        	    if ( getChannel(t)==1){ // 10 is out of bounds anyhow, your index would be 0-9.
            	 	fifo_vec[t][i]=1;
                }
            }
                       
        }
}

void CReadout::Buffer(int g){
     
     for (int t=0;t<channel.size();t++)   {  
            for (int j=0;j<i;j++){ // Where does i come from?
               if(fifo_vec[t][j]==1) { 
                   buffer_ary[g]=1;
                   g++;
               }
    }
     }
       cout<<g<<endl;		  
}


Why do you resize channel in the constructor, then again inside AssignRnd()? I recommend an initializer list for your constructor, use channel.size() instead of passing nchannel around everywhere, and using iterators for your containers wouldn't go astray either.

I fixed most of what people commented 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
82
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <math.h>
#include <map>
#include <utility>
#include <vector>
#include <string>

using namespace std;




CReadout::CReadout( int nchannel, double hitprob ) {
    
    channel.resize( nchannel );
    fifo_vec.resize(channel.size() );
    for( int m = 0; m < 10; m ++ ) {
        
        AssignRnd( hitprob );
        loadFifo_vec( i );
        Buffer( g, i );
        CFifo( i );
        if (i==10) i=0;
        else i++;   
    }
}
CReadout::~CReadout( ) {
	
}

void CReadout::AssignRnd(double hitprob){ 
   
  double random;
     for( int a=0; a<channel.size(); a++) { 
	  random = rand() % 10000;
	  double value = random/10000;  
	
    if( value < hitprob ) channel[a]=1 ;
	else channel[a]=0;
   
	   }
}
  


void CReadout::loadFifo_vec(int i){
	      
	
		for (int t=0;t<channel.size();t++) {
             if (fifo_vec[t][9]!=1){    //full statement and set size(kinda the same thing)
        	    if ( getChannel(t)==1){
            	 	fifo_vec[t][i]=1;
                }
            }
                       
        }
 
}

void CReadout::Buffer(int g,int i){
     
     for (int t=0;t<channel.size();t++)   {  
            for (int j=0;j<i;j++){ 
               if(fifo_vec[t][j]==1) { 
                   buffer_ary[g]=1;
                     g++;
               }
            }
     }
      
}

void CReadout::CFifo(int i){
    for(int t=0;t<channel.size();t++){
      if (fifo_vec[t][9]==1){   //////////////////////////////////////////////////adjust fifo size
        fifo_vec.clear();
    }
}
}


Still two things I dont understand, how do i resize the vector which is nested in fifo_vec and why my iterator i++ is not working. For some reason if the value of i goes above 1 the code crashes. I feel like I am not initializing i correctly.
Last edited on
Topic archived. No new replies allowed.