Domanda

I have the following code:

#define NUM_DAYS 65
#define NUM_PERSON 33

int num = 0;

if(NUM_DAYS % NUM_PERSON)
{
    num = NUM_DAYS / NUM_PERSON;
}
else 
{
    uum = NUM_DAY / NUM_PERSON + 1;
}

num = num - 1;

while(num > 0)
{
    //do something here
}

but I got the following lint warning:

Warning 681: Loop is not entered

what is the problem and how to fix it?

È stato utile?

Soluzione

As your code is written, the loop will not be entered. NUM_DAYS % NUM_PERSON will evaluate to true, so num will equal NUM_DAYS / NUM_PERSON. Since we are dealing with ints, 65 / 33 is equal to 1. 1 -1 is 0, so the while condition will fail.

If your code is written as intended (as in, those constants are the values you are expecting to always use), just remove the while loop. It will never be used. If, however, NUM_DAYS or NUM_PERSON may later contain other values, you probably have nothing to worry about. If those specific values aren't important, try setting them to values such that the division will evaluate to something greater than 1.

Altri suggerimenti

#define NUM_DAYS 65
#define NUM_PERSON 33

int num = 0;

if(NUM_DAYS % NUM_PERSON) // we go here, since (NUM_DAYS % NUM_PERSON) > 0
{
    num = NUM_DAYS / NUM_PERSON; // so num = 1 now
}
else 
{
    uum = NUM_DAY / NUM_PERSON + 1;
}

num = num - 1; // num = 0 now

while(num > 0) // num = 0 ! So we don't go in this loop
{
    //do something here
}

That's why you get this warning. The compiler has determined that your loop is useless (with your current #define value).

Because both expression's value in if-else known at compilation time as follows :

In if (num == 0) in else num == 1, So before while loop num value is either 0 (from else), or -1 (from if) that is not greater than 0 that means while -condition is always false known at compilation time. This means while never executes known at compilation time.

Why static inputs in your code? you have given value at compilation time, ask it from user will be correct.

Remove macros and like below:

int num_days, number_persons;
scanf("%d", &num_days);
scanf("%d", &number_persons);

This will work, note statically assigning these 65, 33 values is problem!

@patrickvacek is right. I will extend his explanation.

In integer arithmetic, 65 divided by 33 is almost 2 but not quite. The quotient is 1 with a remainder of 32. C++ lets you access either the quotient or the remainder, as you prefer. For the quotient, 65 / 33 == 1. For the remainder, 65 % 33 == 32. Your code does not ask for the remainder, but only for the quotient. Thus, if (NUM_DAYS % NUM_PERSON) literally means if (1), which in turn means if (true). Thus, the else branch of your if statement is never reached.

You can follow the rest. Moreover, because the logic is comparatively simple (and for other reasons having to do with the preprocessor and compile-time evaluation, which we will not detail here), the compiler can follow the rest. Thus the warning.

Your compiler computes the value of num already since its possible value could be 65 / 33 or 34 which is demoted to 0 since it is an int type or num = num - 1 which is -1, and substitute the value in while ((0 or -1) > 0). That is why compiler warns you about it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top