•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,594 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,669 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 792 | Replies: 3 | Solved
![]() |
•
•
Join Date: Dec 2004
Posts: 458
Reputation:
Rep Power: 4
Solved Threads: 5
[php]
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;
}
[/php]
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
...
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;
}
[/php]
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
... •
•
Join Date: Dec 2004
Posts: 458
Reputation:
Rep Power: 4
Solved Threads: 5
•
•
•
•
Originally Posted by Narue
This thread has been marked solved. What was your solution?
Hello,
Well i think it was lack of coffee!!
[php]
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;
}
[/php]
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:
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++ ) {
/* ... */
} Member of: Beautiful Code Club.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- What's the HARDEST program you've written? (Computer Science and Software Design)
- convert int to string (C)
- werid code bug (C++)
- code bug (C++)
Other Threads in the C++ Forum
- Previous Thread: Maps in VC++ 6.0
- Next Thread: Using C libraries in C++ programs



Linear Mode