int j;
   for ( j= 0 ;j <sizeOfArray  ; j++)
   {
	if( letter == array2[j] )
      {
       ++found ;
       encryption[j] = array2[j];
        
      }
   }
   
if ( array2[j] =='\0' && found!=0)
       {
        cout << "not found" <<endl;
       --looselife;
       cout << "You now have" << looselife << "remianing" <<endl;
         
		}

Can anyone tell me why my code is always doing the 2nd if statement even if the first one is true? I can put an else since its illegal :( ...

This thread has been marked solved. What was your solution?

This thread has been marked solved. What was your solution?

Hello,

Well i think it was lack of coffee!!

int j; 
   for ( j= 0 ;j <sizeOfArray  ; j++) 
   { 
    if( letter == array2[j] ) 
      { 
       ++found ; 
       encryption[j] = array2[j]; 
         
      } 
   } 
    
if ( array2[j] =='\0' && found!=0) 
       { 
        cout << "not found" <<endl; 
       --looselife; 
       cout << "You now have" << looselife << "remianing" <<endl; 
          
        }

As you can see : if ( array2[j] =='\0' && found!=0) ...found wouldnt be equal to 0 if an element is found making this along with the other if true...

>Well i think it was lack of coffee!!
Ahh, the penultimate cause of bugs in programming. ;)

>for ( j= 0 ;j <sizeOfArray ; j++)
This is also suspicious. If sizeOfArray is a constant, and the array holds variable length strings, you could be accessing indeterminate values, which is undefined behavior. I would be happier to see this:

size_t j, len = strlen ( array2 );

for ( j = 0; j < len; j++ ) {
  /* ... */
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.