Caught in a Loop

This is a discussion on Caught in a Loop within the C Programming forums, part of the General Programming Boards category; Hi, I'm working on a program that should do the following: Prompt a user to enter the amount of gallons ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    15

    Caught in a Loop

    Hi,

    I'm working on a program that should do the following:

    Prompt a user to enter the amount of gallons used
    Prompt a user to enter the amount of miles driven
    Give the average miles / gallon

    This program should repeat this process until the user enters -1 as the sentinal value, after which the program gives the overall miles / gallon.

    I've messed the code up quite a bit, from trying to get it to work. What happens is the first prompt prints okay, but after I enter a number (12.8 in this case), the program does nothing. So if I hit enter, it prints the same line again but with the next prompt on the same line. If I hit enter again, I go into a loop. Any help is appreciated! Here's my code:

    Code:
    #include <stdio.h>
    
    int main()
    {
            float average1, average2, gallons;
            int counter,  miles;
    
            counter = 0;
    
            printf( "Enter the gallons used, -1 to end: " );
            scanf( "%f\n", &gallons );
    
            printf( "Enter the miles driven, -1 to end: ");
            scanf( "%d", &miles );
    
            while ( gallons != -1 ){
    
                    printf( "Enter the gallons used, -1 to end: " );
                    scanf( "%f", &gallons );
                    
                    printf( "Enter the miles driven, -1 to end: ");
                    scanf( "%d", &miles );
            
            while ( gallons != -1 ){
    
                    printf( "Enter the gallons used, -1 to end: " );
                    scanf( "%f", &gallons );
            
                    printf( "Enter the miles driven, -1 to end: ");
                    scanf( "%f", &miles );
    }
                    if ( counter != 0 ) {
                    average1 = ( float ) miles / gallons;
                    printf( "The average miles / gallon is %.6f", average1 );
            }
            else
                    printf( "No information was entered\n" );
    
      return 0;
    }
    Last edited by aprilbiz; 07-22-2002 at 04:02 AM.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    15
    I forgot to add that I'm using cc compiler on DigitalUnix...

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your code is horrible to read. Please use [code] ...your code here... [/code] tags. It will keep your indentation and make it much easier for us to read.

    You could break this up into logical steps, perhaps even having a function for each read, which returns a final correct value:

    Code:
    int main ( void )
    {
        float gallons = 0.0, miles = 0.0;
        int done = 0, counter = 0;
    
        do
        {
            gallons += prompt_gallons( );
            miles += prompt_miles( );
            done = prompt_done( );
            counter++;
        } while( !done );
    
        average( gallons, miles, counter );
    
        return 0;
    }
    You do the rest.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    15
    Thank you so much for your response! Could you give me a little more explaination as to the changes you made compared to my code. I'm thinking you've done some shortcuts that I"m not familiar with... Also, I noticed the you used int main ( void ), could you explain this please. Thanks!

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    45

    Smile

    The best way for you to learn is to take Quzah's code, put it in your compiler and run it through a debugger to see what it does.

    Then, if you have any questions, ask.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    15

    Smile

    Right away Sarge!

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    15
    I just ran it in the compiler and got these messages:

    ld:
    Unresolved:
    prompt_gallons
    prompt_miles
    prompt_done
    average


    I'm sure you know a heck of a lot more than me, but that just ain't lookin' right...

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    That means that the functions "prompt_gallons", "prompt_miles", "prompt_done", and "average" are not defined yet.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by aprilbiz
    I just ran it in the compiler and got these messages:

    ld:
    Unresolved:
    prompt_gallons
    prompt_miles
    prompt_done
    average
    I presume you are using Quzah's example code..... if so, you must remember the key thing in his post:
    >You do the rest.

    You will need to write functions (names above) that prompt the user to input a number, and return that number to the calling function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    15
    I understand the "you do the rest" but if you don't know what you're doing, it's kind of hard to do the rest... I was kind of hoping someone would give me hints as to what was wrong with the code I wrote. That would be the most help to me right now. I"m really trying to figure out how to obtain the averages after the user enters each set of prompts for gallons and miles (ex: enter gallons......enter miles.... the miles/gallon is....) Then at the end, get the overall average. If I could solve this problem, I'd be able to solve the rest of my programs.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    > was kind of hoping someone would give me hints
    I'll help you later... if no-one has beaten me to it
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Unregistered
    Guest
    First, in
    Code:
    printf( "Enter the gallons used, -1 to end: " );
            scanf( "%f\n", &gallons );
    the %f\n will prevent your code from progressing any farther. If you remove the \n at least the code will let you enter miles driven.

    But then, you enter a couple of while loops that do nothing more than ask the user to enter numbers into a variable. What Quzah is recommending, is you write some sort of functions that take the user inputs and do something with them while in the do loop.

    The gallons and miles variables become accumulators in his code, until the user is finished, and the done variable is a flag to let the code know it's time to get out of the do loop.

    The average function takes the accumulated inputs and figures out the average.

    Let me know if you need more direction.

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    15
    This is what I have that is taking in the gallons (decimal) and miles (integer). I can't figure out how to get it to average after the first set of prompts (enter gallons...enter miles... average miles/gallon is...)
    Also the average calculation is not working out.


    Code:
    #include <stdio.h>
    
    int main()
    {
            float average, gallons;
            int counter, miles, total;
    
            total = 0;
            counter = 0;
    
            printf( "Enter gallons used, -1 to end: " );
            scanf( "%f", &gallons );
    
            printf( "Enter the miles driven, -1 to end: " );
            scanf( "%d", &miles );
    
            while ( gallons != -1 ){
            total = total + gallons;
            total = total + miles;
            counter = counter + 1;
            printf( "Enter the gallons used, -1 to end: ");
            scanf ( "%f", &gallons );
    
            printf( "Enter the miles driven, -1 to end: "); 
            scanf( "%d", &miles );
    
           }
            
            if ( counter != 0 ) { 
               average = ( float ) total / counter;
            
            printf( "The average miles per gallon is %.4f" );
           }
            
            return 0;
    }

  14. #14
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    In this version, you are using the variable total to keep track of both miles and gallons. That won't work.

    Second, your average is giving you the total divided by the number of times you've asked the user for input.

    Don't you want to find the average mpg for the total number of gallons and miles?

    Try getting the equation to work once prior to putting it into a while loop.

    Once that works, then you can tackle the harder part.

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >printf("The average miles per gallon is %.4f");
    This isn't going to print the right value. Notice there is no variable name, you forgot it
    >printf("The average miles per gallon is %.4f", average);

    From your original criteria, I think you wanted to print mpg for every entry, then show the total mpg. If this is the case, you'll need to add something like this in the while loop:
    >printf ("mpg is: %.4f\n", miles/gallons);
    Then, fix the problem with storing the running total of gallons and miles (as mentioned by gabulldog).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Page 1 of 2 12 LastLast
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. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

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