User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Dec 2004
Posts: 458
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

code bug!

  #1  
Sep 20th, 2005
[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 ...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: code bug!

  #2  
Sep 20th, 2005
This thread has been marked solved. What was your solution?
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Dec 2004
Posts: 458
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: code bug!

  #3  
Sep 20th, 2005
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...
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: code bug!

  #4  
Sep 20th, 2005
>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++ ) {
  /* ... */
}
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC