# Thread: Sentinal will not break me out of a while loop

1. ## Sentinal will not break me out of a while loop

Here is the program:
Code:
```/* mpg.c computes the average miles per gallons for user entered number of trips */

#include<stdio.h>

int main()
{
unsigned int gals;        // gallons of gas
int miles;        // miles driven
float mpg;        // miles per gallon per trip
//int num_trips;    // total number of trips
//float tot_mpg;    // avg mpg per number of trips
float grand_avg_all_trips;         // average gas used for all trips

// initialize
int num_trips = 0;
float tot_mpg = 0;

// process and get data for first trip
printf("%s", "Enter gallons used: -1 to end  ");
scanf("%d", &gals);
printf("%s", "Enter miles driven: ");
scanf("%d", &miles);
printf("The miles per gallon for this trip is %.2f\n", (float) miles/gals);

while (gals != -1)
{
mpg = miles / gals;
num_trips = num_trips + 1;
tot_mpg = tot_mpg + mpg;

// get data for next trip
printf("%s", "Enter gallons used: -1 to end  ");
scanf("%d", &gals);
printf("%s", "Enter miles driven: ");
scanf("%d",&miles);
printf("The miles per gallon for this trip is %.2f\n", (float) miles/gals);

}   // end while loop here

// termination phase if at least one trip entered
if (gals = -1)
{
grand_avg_all_trips = (float)tot_mpg / num_trips;
printf("The average mpg for %d trips is %.2f: \n", num_trips, (float)tot_mpg / num_trips);
}
else
{
printf("No data was entered.");
}
return 0;

}```

/*

Enter gallons used: -1 to end 8
Enter miles driven: 125
The miles per gallon for this trip is 15.62
Enter gallons used: -1 to end 5
Enter miles driven: 115
The miles per gallon for this trip is 23.00
Enter gallons used: -1 to end -1
Enter miles driven: -1
The miles per gallon for this trip is -0.00
Enter gallons used: -1 to end ^C

Enter gallons used: -1 to end 8
Enter miles driven: 125
The miles per gallon for this trip is 15.62
Enter gallons used: -1 to end 5
Enter miles driven: 115
The miles per gallon for this trip is 23.00
Enter gallons used: -1 to end -1
Enter miles driven:
0
The miles per gallon for this trip is 0.00
Enter gallons used: -1 to end -1
Enter miles driven: -1
The miles per gallon for this trip is -0.00
Enter gallons used: -1 to end

*/

2. while(gals != -1)

This will never be true because gals is an unsigned int.

Add a simple print statement after L23 to see the value of gals when you enter -1

3. You're also mixing up = vs ==

if (gals = -1)

4. Thanks Thmm and Salem for pointing me in the right direction. I made some changes and can now complete the program but I need to enter -1 for both gals and miles. I tried if gals and if gals or if miles but I can't avoid entering -1 twice. Can this be done with one if ? Thanks again for your help.

Code:
```/* mpg.c computes the average miles per gallons for user entered number of trips */

#include<stdio.h>

int main()
{
unsigned int gals;		// gallons of gas
int miles;		// miles driven
float mpg;		// miles per gallon per trip
//int num_trips;	// total number of trips
//float tot_mpg;	// avg mpg per number of trips
float grand_avg_all_trips; 		// average gas used for all trips

// initialize
int num_trips = 0;
float tot_mpg = 0;

// process and get data for first trip
printf("%s", "Enter gallons used: -1 to end  ");
scanf("%d", &gals);
printf("%s", "Enter miles driven: ");
scanf("%d", &miles);
printf("The miles per gallon for this trip is %.2f\n", (float) miles/gals);

while (gals != -1)
{
mpg = miles / gals;
num_trips = num_trips + 1;
tot_mpg = tot_mpg + mpg;

// get data for next trip
printf("%s", "Enter gallons used: -1 to end  ");
scanf("%d", &gals);
printf("%s", "Enter miles driven: ");
scanf("%d",&miles);
printf("The miles per gallon for this trip is %.2f\n", (float) miles/gals);

}   // end while loop here

// termination phase if at least one trip entered
if (gals == -1 || miles == -1)
{
grand_avg_all_trips = (float)tot_mpg / num_trips;
printf("The average mpg for %d trips is %.2f: \n", num_trips, (float)tot_mpg / num_trips);
}
else
{
printf("No data was entered.");
}
return 0;

}

/*

Enter gallons used: -1 to end  8
Enter miles driven: 125
The miles per gallon for this trip is 15.62

Enter gallons used: -1 to end  5
Enter miles driven: 115
The miles per gallon for this trip is 23.00

Enter gallons used: -1 to end  12
Enter miles driven: 400

The miles per gallon for this trip is 33.33
Enter gallons used: -1 to end  -1
Enter miles driven: -1

The miles per gallon for this trip is -0.00
The average mpg for 3 trips is 23.67:

*/```

5. Originally Posted by mack99
I made some changes and can now complete the program but I need to enter -1 for both gals and miles. I tried if gals and if gals or if miles but I can't avoid entering -1 twice. Can this be done with one if ? Thanks again for your help.
I assume you want to run the program at least once. Test the value of gals, will get what you want:
Code:
```  // get data for next trip
printf("%s", "Enter gallons used: -1 to end  ");
scanf("%d", &gals);
if(gals < 0)          // Signal to end program
break;
printf("%s", "Enter miles driven: ");
scanf("%d",&miles);
printf("The miles per gallon for this trip is %.2f\n", (float) miles/gals);```
You need to test the return calls to scanf() and deal with removing the garbage from the input buffer. What if I enter, "abc" to any of the scanf() statements.

Also, why is gals, an unsigned int? Why not a floating point value? Do you always use full galls (2), or partial galls, (1.5)?

6. I would use this structure
Code:
```// compile with -std=c99

#include <stdio.h>
#include <stdbool.h>

int main()
{
int gals = 0;      // gallons of gas
int miles = 0;      // miles driven
float mpg = 0;      // miles per gallon per trip
float grand_avg_all_trips = 0;      // average gas used for all trips

while(true)
{
printf("%s", "Enter gallons used: -1 to end  ");
scanf("%d", &gals);
if(gals == -1)
break;
printf("%s", "Enter miles driven: ");
scanf("%d", &miles);
printf("The miles per gallon for this trip is %.2f\n", (float) miles/gals);
}
printf("Thanks for using this app.\nGood bye");

}```
Output:
Enter gallons used: -1 to end 2
Enter miles driven: 15
The miles per gallon for this trip is 7.50
Enter gallons used: -1 to end 5
Enter miles driven: 66
The miles per gallon for this trip is 13.20
Enter gallons used: -1 to end -1
Thanks for using this app.
Good bye

Better would be to use floats for gals and miles, unless this is some kind of homework and floats are not required.

7. Thanks again Thmm, Salem and Rstanley. Float does make for more accuracy with gals and miles. No homework just a retiree mucking about with C for fun and pastime