Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop using strcmp()

Status
Not open for further replies.

Denster

Programmer
Apr 9, 2001
198
GB
I am doing an assignment that requires two input files to be read a record at a time. the loop can only terminate when both codes read "99999". The code I have should produce an infinite loop but ignores the second value.

#include <stdio.h>
#include <string.h>

void main()
{
char a[] = &quot;00000&quot;,*a_ptr;
char b[] = &quot;00000&quot;,*b_ptr;
while((strcmp(a,&quot;99999&quot;)!=0)&&(strcmp(b,&quot;99999&quot;)!=0))
{
/* rest of the coding will go here */
strcpy(a,&quot;99999&quot;);
strcpy(b,&quot;99990&quot;);
}
}

 
because of the && when strcmp(a,&quot;99999&quot;) is met it stops

I dont know what you are trying to test but this is the reason why it is not infinte.

Matt
 
yes I know this is the reason but what is the solution?. I need to exit the loop when both a & b are equal to 99999.
 
Oh :) sorry

here ya go

while( !(!strcmp(a,&quot;99999&quot;) && !strcmp(b,&quot;99999&quot;)) )


btw. I'm sorry but I look at things differently. This could be rewritten as strcmp(...) == 0. Also note that I used the ! again to fix up the problem.

Matt
 
thats it! I've put it into my main prog and it works perfectly. Thanks for the info.



Dennis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top