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

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    18

    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. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    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. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,430
    You're also mixing up = vs ==

    if (gals = -1)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    18
    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. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,010
    Quote Originally Posted by mack99 View Post
    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)?
    Last edited by rstanley; 07-08-2022 at 09:58 AM.

  6. #6
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    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. #7
    Registered User
    Join Date
    Aug 2012
    Location
    Florida
    Posts
    18
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Averaging numbers using a sentinal to stop the loop
    By jesterisdead in forum C Programming
    Replies: 9
    Last Post: 04-13-2014, 02:05 AM
  2. c# help needed with a mix of loop terminated sentinal
    By xbox221 in forum C# Programming
    Replies: 6
    Last Post: 01-10-2011, 09:23 PM
  3. break from for loop
    By taurus in forum C Programming
    Replies: 3
    Last Post: 09-24-2009, 03:54 PM
  4. how could I break this loop?
    By Trafalgar Law in forum C Programming
    Replies: 4
    Last Post: 09-27-2008, 05:11 AM
  5. Loop will not break.
    By silhoutte75 in forum C Programming
    Replies: 1
    Last Post: 03-20-2008, 06:51 PM

Tags for this Thread