2012-12-11 2 views
-1

저는 C++을 처음 접했고 올바르게 설명하고 있는지 확신 할 수 없습니다.C++ if 문이 끝나지 않을 것입니다.

전체 프로그램을 실행할 때 끝나지 않는 코드 조각 (기능)이 있습니다.

이 함수의 목적은 먼저 0에서 1 사이의 난수를 생성하는 것입니다. 그 난수가 0.6보다 큰 경우 다른 함수로 이동합니다. 그 함수 virus_death는 완벽하게 작동합니다. 난수가 0.6보다 작 으면 바이러스 포인트를 도달 된 벡터로 밀어 넣습니다. 또한 포인트를 파일에 씁니다. 그러나 그 문장은 결코 멈추지 않습니다.

void replication(vector<Virus>& virus, int a, int b, double& times, int& iv, 
       ofstream& data_file, int p, vector<Direction>& direction_v, 
       vector<Virus>&replicate, int step) 
{ 
    point R,P; 

    double replicates = ((double) rand()/(RAND_MAX)); 
    //how long the virus takes to reach the necleus 

    if(replicates>0.6) 
    { 
     cout<<"No Replication "<<endl; 
     virus_death(times, iv, virus,a,b,data_file,p,direction_v, step); 
    } 
    else if(replicates<=0.6) 
    { 
     cout<<"Replication"<<endl; 

     ofstream tom; 
     tom.open("Timing_nuc.dat"); 

     cout<<"Points"<<endl; 
     R.x = virus[p].getx(); 
     R.y = virus[p].gety(); 
     R.t = virus[p].gett(); 

     cout<<"Write to File"<<endl; 
     tom<<p+1<<"\t"<<R.x<<"\t"<<R.y<<"\t"<<R.t<<"\n"; 

     cout<<"New Points"<<endl; 
     P.x = a; 
     P.y = b; 
     P.t = 0; 
     cout<<P.x<<"\t"<<P.y<<"\t"<<P.t<<"\n"; 


     replicate.push_back(P); 
     int aspa = replicate.size(); 
     cout<<"Replication Size:"<<aspa<<endl; 
    }   
} 

사람은 포인트가 벡터에 밀려되면,이 기능을 떠나지 않을 것 왜 나를 결정하는 데 도움이 바랍니다 수 있습니다. 이 함수를 호출

코드입니다 : 당신은 당신이 어딘가에 무한 재귀가없는 확인

void location_of_virus(vector<Virus>&virus,double& times, int& iv, 
         ofstream& data_file, vector<Direction>& direction_v, 
         vector<Virus>&virus_engulf, circle&cell_c, 
         circle&nuclei_c, vector<Virus>&replicate, int step) 
{ 
    int a; 
    int b; 

    //int nc,cc,cmc,nci; 
    float dis_c, dis_n; 

    int virus_size= virus.size(); 
    //type = "virus"; 
    for(int i =0; i<virus_size; i++) 
    { 
     a = virus[i].getx(); 
     b = virus[i].gety(); 

     //to check where the virus is in location to the environment 
     dis_c = cell_c.distance(a,b); 
     dis_n = nuclei_c.distance(a,b); 

     if(dis_n < 5) 
     { 
      location =" Inside Nucleus"; 
      cout<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<endl; 
      data_file<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<"\n"; 
      replication(virus,a,b,times,iv,data_file,i,direction_v,replicate,step); 
     } 
     else if(dis_n ==5) 
     { 
      location = "Nucleus"; 
      cout<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<endl; 
      data_file<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<"\n"; 
      nucleus(virus,a,b,times,iv,data_file,i,direction_v,step); 

     } 
     else if(dis_c <25 && dis_n>5) 
     { 
      location = "Cytoplasm"; 
      cout<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<endl; 
      data_file<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<"\n"; 
      cytoplasm(virus,a,b,times,iv,data_file,p,direction_v,step); 
     } 
     else if(dis_c ==25) 
     { 
      location = "Cell Membrane"; 
      cout<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<endl; 
      data_file<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<"\n"; 
      endocytosis(virus,a,b,times,iv, data_file,i,direction_v,virus_engulf,cell_c,nuclei_c); 
     } 
     else if (((a ==0 || a == 50 || a>0 || a<50)&& (b ==0 || b == 50 || b>0 || b<50)) && dis_c>15) 
     { 
      location ="Extracellular Matrix"; 
      cout<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<endl; 
      data_file<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<"\n"; 
     } 
     else 
     { 
      location ="Left the System"; 
      cout<<i+1<<"\t"<<virus[i]<<"\t"<<location<<endl; 
      data_file<<i+1<<"\t"<<virus[i]<<"\t"<<location<<"\n"; 

     } 
    } 
} 
+9

여기에 루프가 없습니다. – chris

+5

루프? 나는 어떤 고리도 보지 않는다. –

+2

"루프를 굽히지 마십시오. 불가능합니다 ..." – Boann

답변

1

있습니까? 예 : 어쩌면 virus_deathlocation_of_virus으로 다시 전화할까요?

또한 virus[p]이 유효한지 확인하지 않아도됩니다. p이 범위 내에 있으면 작은 안전 점검을 추가 할 수 있습니다.

어떻게 든 어딘가에서 스택을 엉망으로 만들면 모든 종류의 이상한 일이 발생할 수 있습니다.

+0

방금 ​​무한 재귀가없고 바이러스 [p]가 유효한지 확인했습니다. – user1572147

+0

다른 곳에서 내 스택을 엉망으로 만드는 것이 무슨 뜻인지 자세히 설명해 주시겠습니까? – user1572147

+0

무한 재귀가 없음을 어떻게 확인 했습니까? Btw., 디버거에서 단계별로 진행될 때 무엇을 볼 수 있습니까? 스택을 엉망으로 만들 때 잘못된 메모리 주소를 쓸 때 스택이 발생합니다. valgrind와 같은 도구가이를 감지 할 수 있습니다. – Albert

관련 문제