I need to know how to make this ignore the number 0 when 0开发者_StackOverflow社区 is input so that the program does not exit when 0 is input.
#include <stdio.h>
int main()
{
int input = 0, previous = 0;
do {
previous = input;
printf("Input Number");
scanf("%d", &input);
} while( input!= previous*2 );
return 0;
}
Pick a different value for previous
. Try INT_MAX >> 1
from limits.h.
#include <stdio.h>
int main()
{
int input = 0, previous = 0;
do {
previous = input;
printf("Input Number");
scanf("%d", &input);
} while( input == 0 || input!= previous*2 );
return 0;
}
I added an OR operator, that means that if input is equal to 0, then it will still continue, but, if it is not 0, and the second condition is satisfied, it will break the loop and exit.
#include <stdio.h>
int main()
{
int input = 0, previous = 0;
do {
previous = input;
printf("Input Number");
scanf("%d", &input);
} while( input!= previous*2 || input==0);
return 0;
}
while( input == 0 || input!= previous*2 ) in plain english this reads: while input equals zero or input is not equal to twice the previous. || is a logical or operation, and != (which was in your original code by the way) means not equal to.
精彩评论