# Caught in a Loop

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-22-2002
aprilbiz
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; }```
• 07-22-2002
aprilbiz
I forgot to add that I'm using cc compiler on DigitalUnix...
• 07-22-2002
quzah

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.
• 07-22-2002
aprilbiz
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!
• 07-22-2002
gabulldog
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. ;)
• 07-22-2002
aprilbiz
Right away Sarge!:)
• 07-22-2002
aprilbiz
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...
• 07-22-2002
Gabu
That means that the functions "prompt_gallons", "prompt_miles", "prompt_done", and "average" are not defined yet.
• 07-22-2002
Hammer
Quote:

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.
• 07-22-2002
aprilbiz
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.
• 07-22-2002
Hammer
> was kind of hoping someone would give me hints
I'll help you later... if no-one has beaten me to it :)
• 07-22-2002
Unregistered
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.
• 07-22-2002
aprilbiz
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; }```
• 07-22-2002
gabulldog
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.;)
• 07-22-2002
Hammer
>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).
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last