Loop will not complete

This is a discussion on Loop will not complete within the C Programming forums, part of the General Programming Boards category; ok all I'm a new c student. So I need eveeryone who post not to use advanced functions I haven't ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    23

    Loop will not complete

    ok all I'm a new c student. So I need eveeryone who post not to use advanced functions I haven't been instructed in.
    here is the homework exercise:

    develop a program that will input the miles driven and gallons used for each tankful. the program should calculate and display the miles per gallon obtained for each tankful. after processing all input, the porgram should calculate and print the combined miles per gallon from all tankfuls.

    I've got the program to work on the first inputs and it diplays my mpg and then asks for next gallon input. if I do -1 it keeps on asking for next input and never ends giving me a grand total mpg for one or more tankfuls.

    here is my code:
    Code:
    /* miles per gallon on each tank full */
    #include <stdio.h>
    
    int main()
    {
    	int miles; /* miles driven */
    	float mpg, gallons, total; /* miles per gallon, gallons used, total of all mpg's */
        int counter; /* the counter */
        
    	counter = 0;
    	mpg = 0;
    	total = 0; /* intitilizing all non user input veriables */
    	
    	printf ("Enter the gallons used (-1 to end):");
    	scanf  ("%f", &gallons); /* user input of the gallons used */
    
    	printf ("Enter the miles driven:");
        scanf  ("%d", &miles ); /* user input of the miles driven */
    
    	mpg = miles / (float) gallons; /* formula to obtian miles per gallon based on inputs */
    
    	printf ("the miles/gallon for this tank was: %.2f\n" , mpg); /* result on first avrage before moving to next prompt*/
    
    	while ( gallons != -1) { /* loop until the input of -1 @ gallons prompt */
            
    		total = total + mpg;
           
            counter++; 
    		
    		
    		printf ("Enter the gallons used (-1 to end):");
    		scanf  ("%f", &gallons); 
    		printf ("Enter the miles driven:");
    		scanf  ("%d", &miles );
            printf ("the miles/gallon for this tank was: %.2f\n" , mpg);
    		
    	} /* end while */
    
    	if ( counter != 0 ) { 
               mpg = ( float ) total / counter; /* grand total */
            
            printf( "The average miles per gallon is %.2f",mpg );
    	}/* end if */
    
    	else { /* if no input but -1 was entered */
    		printf ("No information entered");
    	} /* end else */
    
    	return (0);
    }
    /* end main */
    Remember I'm new and very limited in shortcuts so please use the most basic forms of programming in helping me find the code's problems.

    Thanks all,
    Joseph

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Don't post two identical threads: how to convert decimal number to ASCII code?

    Your code would ask for the miles driven even if -1 gallons were entered. You should put a test for -1 gallons right after you read the gallons from the user. The break statement might be useful in this case.

    You also need to calculate mpg in the loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    i did the mpg in the loop fixed some of it and i've exited the program with a total but its way off
    any ideas here is the new code
    Code:
    /* miles per gallon on each tank full */
    #include <stdio.h>
    
    int main()
    {
    	int miles; /* miles driven */
    	float mpg, gallons, total; /* miles per gallon, gallons used, total of all mpg's */
        int counter; /* the counter */
        
    	counter = 0;
    	mpg = 0;
    	total = 0; /* intitilizing all non user input veriables */
    	
    	printf ("Enter the gallons used (-1 to end):");
    	scanf  ("%f", &gallons); /* user input of the gallons used */
    
    	printf ("Enter the miles driven:");
        scanf  ("%d", &miles ); /* user input of the miles driven */
    
    	mpg = miles / (float) gallons; /* formula to obtian miles per gallon based on inputs */
    
    	printf ("the miles/gallon for this tank was: %.2f\n" , mpg); /* result on first avrage before moving to next prompt*/
    
    	while ( gallons != -1) { /* loop until the input of -1 @ gallons prompt */
            
    		total = (float)total + (float)mpg;
            counter = counter + 1; 
    		
    		
    		printf ("Enter the gallons used (-1 to end):");
    		scanf  ("%f", &gallons); 
    		
    		printf ("Enter the miles driven:");
    		scanf  ("%d", &miles );
            
    		mpg = miles / (float) gallons;
    
    		printf ("the miles/gallon for this tank was: %.2f\n" , mpg);
    		
    	} /* end while */
    
    	if ( counter != 0 ) { 
               mpg = ( float ) total / counter; /* grand total */
            
            printf( "The average miles per gallon is %.2f", total );
    	}/* end if */
    
    	else { /* if no input but -1 was entered */
    		printf ("No information entered\n");
    	} /* end else */
    
    	return (0);
    }
    /* end main */
    thanks,
    joe

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You still need to do something immediately after the scanf() if the user enters -1 for the gallons. Perhaps something like this:
    Code:
    for(;;) {
        printf("Enter the gallons used (-1 to end): ");
        scanf("%f", &gallons);
        if(gallons == -1) break;
        /* ... */
    }
    You also have repeat code before and inside your while loop which could be reduced to one instance of the code.

    All of your float casts that I can see are superfluous.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    thank you DWK found the problem it was in the last if function on the fomula it should be
    Code:
    if ( counter != 0 ) { 
               total = ( float ) total / counter; /* grand total */
            
            printf( "The average miles per gallon is %.2f", total );
    	}/* end if */
    not like before
    Code:
    if ( counter != 0 ) { 
               mpg = ( float ) total / counter; /* grand total */
            
            printf( "The average miles per gallon is %.2f", total );
    	}/* end if */
    changed that and the program functions perfect.

    thanks again,
    joe

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I see two problems, Joseph. First is getting out of the while loop, and the second is your arithmetic on total mpg.

    Your code needs this added right after the user input of the gallons used:
    Code:
    if ( gallons < 0 ) break;
    Then your arithmetic. You have total being a sum of total + mpg. Which is no good. You need to have a variable totalmiles, to keep a total of all the miles travelled, AND you need a totalgallons to keep a sum of the gallons that were used.

    Intermediate mpg's, should be ignored after they've been figured, and shown to the user. You just want the totalmiles traveled, and totalgallons of gas used.

    THEN the figuring for this is no sweat.

    Pretty simple, eh?

    And welcome to the forum, Joseph.



    Adak

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Threads merged.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    Thanks Adak :-)
    that extra break code makes for a smooth program.

    Joseph

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21