I am writing this program about Gas mileage, In the program I am assigning a value that is -1 as a sentinel value, that's why I use the While loop. The problem is I can input gallon variable, but the miles variable keep repeating over and over again.

My question is how can I have the mile variable repeated only one time in the loop? So that I can solve it.

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int gallon;
  int miles;
  int total;

  total = 0;

  printf( "Enter gallon used (-1 to end): " );
  scanf( "%d", &gallon);

while( gallon != -1 ) {
   total = miles / gallon;

    printf( "Enter miles driven" );
    scanf("%d", &miles);
}
    return 0;
}

Recommended Answers

All 8 Replies

gallon is used as the loop controller, but you never change its value in the loop body. This is called an infinite loop.

gallon is used as the loop controller, but you never change its value in the loop body. This is called an infinite loop.

Thanks for your response. Can you rewrite the code segment where the while loop is so that I can understand what you mean?

No. Read what deceptikon said, work at understanding his meaning, and fix your code.

Or try following your code with paper and pencil to see what the values are doing. This is called debugging and is a critical component of programming.

No. Read what deceptikon said, work at understanding his meaning, and fix your code.

Or try following your code with paper and pencil to see what the values are doing. This is called debugging and is a critical component of programming.

I am a little bit familiar with counter. When it's used it must be assigned a number such 0, or 1. For instance when it comes down to gallon must I assign it a number just like counter?

I repeat

... try following your code with paper and pencil to see what the values are doing. This is called debugging and is a critical component of programming.

WaltP and Deceptikon are right. And I can tell they're ready to lose their patience. It's a simple fix, but to a new coder, it may not be obvious.

See this line of code?

printf( "Enter gallon used (-1 to end): " );
  scanf( "%d", &gallon);

This is the only time you change the gallon variable. It is used elsewhere, but not changed.
The while loop depends on the gallon variable. If the while loop is processing and the gallon variable is not changing, the while loop will loop forever. So the question becomes "do you really want that block of code where it's at or would it be more useful somewhere else?"

WaltP and Deceptikon are right. And I can tell they're ready to lose their patience. It's a simple fix, but to a new coder, it may not be obvious.

See this line of code?

printf( "Enter gallon used (-1 to end): " );
  scanf( "%d", &gallon);

This is the only time you change the gallon variable. It is used elsewhere, but not changed.
The while loop depends on the gallon variable. If the while loop is processing and the gallon variable is not changing, the while loop will loop forever. So the question becomes "do you really want that block of code where it's at or would it be more useful somewhere else?"

Well, I keep moving the code blocks around. It does not work. How do I change the gallon variable since all I can do is Input number. Gallon does not have a specific value that is assigned to it.

Well, I keep moving the code blocks around. It does not work.How do I change the gallon variable since all I can do is Input number. Gallon does not have a specific value that is assigned to it.

printf( "Enter gallon used (-1 to end): " );
  scanf( "%d", &gallon);

did you try putting these statements inside the loop....

With all the replies I think enough hint is given already :-/

commented: Thanks, ZeroLiken. +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.