while loop give result back when one of the condition succeed, i need both of them, any help
#include <stdio.h>
int yRechner()
{
int x = 2;
int y =0;
int i =0;
char answer[2];
while (i < 1 && y <= x)
{
printf("is the wall here pleas answer yes(Y/y) or No(N/n) \n");
scanf("%s", &answer);
if (answer[0] == 'N' || answer[0] == 'n')
{
printf("one step \n");
y++;
}
else if (answer[0] == 'Y' || answer[0] == 'y')
{
if (i < 1 && y > x)
{
i++;
printf("rotate \n");
}
else
{
printf("am not seeing the wall please say no. \n");
}
}
else
{
printf("wrong answer \n");
}
}
return y;
}
int main()
{
int y = yRechner();
printf("y = %d\n", y);
return 0;
}
It isn't apparent whether these conditions, i < 1
and y <= x
, are negated or not. In general, if you want to loop while not both of the conditions are true, !(c1 && c2)
, you can use De Morgan's laws to simplify !c1 || !c2
!c1 || !c2
.
I found a Solution, thx for your hints
#include <stdio.h>
int yRechner()
{
int x = 2;
int y =0;
int i =0;
char answer[2];
while (i < 1 )
{
printf("is the wall here please answer yes(Y/y) or No(N/n) \n");
scanf("%s", &answer);
if (answer[0] == 'N' || answer[0] == 'n')
{
printf("one step \n");
y++;
}
else if (answer[0] == 'Y' || answer[0] == 'y')
{
if (i < 1 && y > x )
{
i++;
printf("rotate \n");
continue;
}
else if (i < 1 && y < x)
{
printf("am not seeing the wall please say no. \n");
continue;
}
}
else
{
printf("wrong answer \n");
}
}
return y;
}
int main()
{
int y = yRechner();
printf("y = %d\n", y);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.