Page 1 of 1

do while bug ?

PostPosted: Tue Jul 29, 2008 6:37 am
by _Maxi
Hi.

I have an if inside a do while. And it doesnt do what its supposed to do.

Check:

Code: Select all
do {
      cant = gets(" number of players (1-9): ") ;
      if( (cant<=0) && (cant>=10) ) //invalid
      {   flag2 = 1 ;
         puts("\n\n Between 1 and 9, put again.\n\n") ;
      }
      else
         flag2 = 0 ; // everything ok, goes out
      
   }  while (flag2==1) ; //asks again


It never detecs if cant is invalid. I put invalid cant and it gets out of the do while anyway, without entering the if. It just acts like cant is valid when its not.

Whats wrong?

edit: Well... the problem may be that PocketC doesnt support complex if like I used with &&, I'll change that and report back.

edit2: yes, the problem was the complex if. I used two ifs and now it works.
Thanks.

Re: do while bug ?

PostPosted: Thu Jul 31, 2008 10:38 pm
by Percival
_Maxi wrote:
Code: Select all
if( (cant<=0) && (cant>=10) ) //invalid


This is never true, because it is impossible to be smaller equal to 0 AND larger equal to 10. You should have used OR instead.
Code: Select all
if( (cant<=0) || (cant>=10) ) //invalid

Re: do while bug ?

PostPosted: Fri Aug 01, 2008 1:15 pm
by _Maxi
Hahahaa..... you are 100% right !! Thanks.