# Thread: Problem with sentinel values

1. ## Problem with sentinel values

I am working independently through Deitel's How to Program C and I am having some difficulty with sentinel values. The problem is that the sentinel value doesn't stop the program, but instead uses the sentinel value( in this case -1) in the calculation. This leads to averages that are negative or far from the correct value. Please help. Here is what I have so far, if it helps.

also - I am using visual c++ 2005 express addition, and i am constantly having to insert the first piece of code (#define _CRT_SECURE_NO_DEPRECATE)
because visual c++ wants me to use scanf_s instead of scanf. Is this normal?

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

int main( void )
{
int miles, gallons, totalmiles, totalgallons;
float mpg, totalmpg;

totalmiles = 0, totalgallons = 0;

printf( "Enter the gallons used (-1 to end): " );
scanf( "%d", &gallons );

totalgallons = totalgallons + gallons;

printf( "Enter the miles driven: " );
scanf( "%d", &miles );

totalmiles = totalmiles + miles;

mpg = ( float )miles / gallons;

printf( "The miles/gallon for this tank was %.6f\n", mpg );

while ( gallons != -1 ) {
printf( "Enter the gallons used (-1 to end): " );
scanf( "%d", &gallons );

totalgallons = totalgallons + gallons;

printf( "Enter the miles driven: " );
scanf( "%d", &miles );

totalmiles = totalmiles + miles;

mpg = miles / gallons;
printf( "The miles / gallon for this tank was %.6f\n", mpg );
}

if ( totalmiles != 0 ) {
totalmpg = ( float )totalmiles / totalgallons;

printf( "The overall average miles/gallon was %.6f\n", totalmpg );
}
else {
printf( "No gallons were entered\n" );
}

return 0;
}```

2. Yes, of course it uses the -1 in the calculation (and it asks for a number of miles too, right?)

You need to add a check to see if the gallons is -1 and only do the rest of the loop if it is. A while-loop doesn't test the value at every instance the value is changed, only when it reaches the top of the loop.

--
Mats

3. also - I am using visual c++ 2005 express addition, and i am constantly having to insert the first piece of code (#define _CRT_SECURE_NO_DEPRECATE)
because visual c++ wants me to use scanf_s instead of scanf. Is this normal?
I have heard that MSVC++ 2005 express complains whenever you use scanf() or sprintf() or other standard C functions. So yes, it's normal. I didn't know that you could disable it with a #define, though.

Code:
```	while ( gallons != -1 ) {
printf( "Enter the gallons used (-1 to end): " );
scanf( "&#37;d", &gallons );

totalgallons = totalgallons + gallons;

/* ... */
}```
You read in gallons, then go on to process it, assuming it is a valid number; then, after all of the calculations have been performs, and only then, is the expression in the while evaluated and found to be false.

You could fix this with a break statement:
Code:
`if(gallons == -1) break;`
Or you could wrap if(gallons != -1) around the following code.

 A bit late, but oh well. [/edit]

4. Code:
```	printf( "Enter the gallons used (-1 to end): " );
scanf( "%d", &gallons );

totalgallons = totalgallons + gallons;

printf( "Enter the miles driven: " );
scanf( "%d", &miles );

totalmiles = totalmiles + miles;

mpg = ( float )miles / gallons;

printf( "The miles/gallon for this tank was %.6f\n", mpg );```
It looks like you've duplicated this code twice, once outside the loop and once inside. If you get rid of the instance outside the loop, you could then add a check for
Code:
```if (gallons < 0) {
printf ("exiting\n");
break;
}```
just after scanf. You should avoid code duplication wherever possible!