954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Concerning the while loop

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;
}
arold10
Newbie Poster
24 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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

deceptikon
Indubitably
Administrator
632 posts since Jan 2012
Reputation Points: 119
Solved Threads: 105
 
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?

arold10
Newbie Poster
24 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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?

arold10
Newbie Poster
24 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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 thegallon 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?"

DeanMSands3
Junior Poster
185 posts since Jan 2012
Reputation Points: 37
Solved Threads: 26
 

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 thegallon 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.

arold10
Newbie Poster
24 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 
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 :-/

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: