# Thread: While loop with multiple conditions.

1. ## While loop with multiple conditions.

My code's while loop has two values in it, a weight and a value. my sentinel value is "-1". I have tried to modify the conditions in the while loop to everything I can think of but I'm at a loss. When I enter my sentinel value in my standard input, it ends the loop but also factors in the sentinel value in the average. below is the code:

Code:
```<#include <stdio.h>

int main (void) {
/* variable definition: */
unsigned int counter;
int count;
double avg, value, weight, sum, sumw;
/* Initialize */
counter =0;
count = 0;
sum = 0;
sumw = 0;
avg = 0.0;
// Loop through to input values
while (value != -1)
{
printf("Enter a value and its weight, -1 to end: ");
// use %lf for double, %f for float
scanf("%lf %lf", &value, &weight);
printf("%.2lf (value) %.2lf (weight)\n", value, weight);
if (weight != 0) {
sumw = sumw + weight;
sum = sum + value * weight;
count = count + 1;
}
else {
printf("Weight must be positive\n");
} // end if weight ok
} // end reading input values and weights
// Calculate avg if sumw is not 0
avg = sum / sumw;
printf("average is %lf\n " , avg );
return 0;
} // end main>```

and below is the output:

2 1 2 1 -1
stdout copy
Enter a value and its weight, -1 to end: 2.00 (value) 1.00 (weight)
Enter a value and its weight, -1 to end: 2.00 (value) 1.00 (weight)
Enter a value and its weight, -1 to end: -1.00 (value) 1.00 (weight)
average is 1.000000

I have tried to modify the code to read:

while (value && weight != -1)
While (value || weight != -1)
while (value, weight =! -1)
while (value != -1)
while (weight != -1)
While ((value) (weight) != -1)
while ((value), (weight) != -1)

none of these seem to work. 2. Why do you have this code?

Code:
`if (weight != 0) {`

Code:
`if (weight > 0) {`
Tim S. 3. You do realize that you need to set weight to zero before the scanf, right?
Because if you do not enter a value it stays what it was before.

Tim S. 4. I had this code there simply because I attempted to change so many things in the code before posting here, I forgot to change it back. I am painfully new at this btw. 5. I set weight=0 under the initialization, I assumed this is what you meant. I also changed the "!=" back to ">". the results are still the same as before. instead of the program terminating after -1, the program is factoring in the -1 as a value and entering in a weight of 1 AND terminating the program, instead of just simply terminating it. If my input was 2 1 2 1 2 1-1, i should be getting the average = 2, instead I'm getting 1.25. Im sure there is an easy or obvious fix but I'm not sure how much longer I can butcher this code before figuring it out... 6. No, that is not what I meant.

Tim S. 7. Code:
```weight = 0;
scanf("%lf %lf", &value, &weight);``` 8. I don't see the point of assigning 0 to weight just before the scanf: it will typically get overwritten immediately, and if it doesn't, that's because of an input error. Instead, check the return value of scanf before proceeding to use value and weight. 9. Originally Posted by laserlight I don't see the point of assigning 0 to weight just before the scanf: it will typically get overwritten immediately, and if it doesn't, that's because of an input error. Instead, check the return value of scanf before proceeding to use value and weight.
That is the more correct way, yes. But, my way is simpler for newbies.
It depends on whether to teach the more correct way or the simple way is better in this instance.
I think either will work; but, I have no idea which the OP is likely to understand.

Tim S. Popular pages Recent additions avg, end, sentinel value, weight, while loop 