Click to See Complete Forum and Search --> : C Question On Looping with While
JockVSJock
02-02-2003, 12:40 PM
Still working on C Stuff here and I am doing a program with a While loop. What I want it to do is an infinint loop, but break out of it, if an end-user enter -1 for either entry.
So far, it will loop three times and not output the number that I am looking for. Here is the code:
#include <stdio.h>
main()
{
float fGallons = 0;
float fMilesDriven = 0;
float fOutput = 0;
while (fGallons != -1 || fMilesDriven != -1) {
printf("Enter -1 to quit anytime \n");
printf("Enter the gallons of gas used \n");
scanf("%f", &fGallons);
printf("Enter the miles driven \n");
scanf("%f", &fMilesDriven);
fOutput = fMilesDriven / fGallons;
}
printf("The miles / gallons used was %f \n", fOutput);
return 0;
}
Any advise on what to do or improve on here?
thanks
-Chris
zdude255
02-02-2003, 01:14 PM
Well first lets indent the while loop.
Then we can put something like this after each input:
if (lastinput == -1){
break;
}
bwkaz
02-02-2003, 01:48 PM
And you can make the while condition a little more explicitly infinite by changing it to while(1) {
That way, the break will still end the loop, but it's a little more easily recognizable as an infinite while loop.
And you're probably going to want to put the last printf inside the loop, so that each time the loop gets executed, the output gets printed.
PolteRGeisT
02-02-2003, 03:05 PM
This is just a comment on your declaration of main():
For the sake of portability, you should declare the return type of main(). the new C99 standard states that you MUST do this, so you will see future compilers refusing to compile code without return types specifically stated. I suggest you get in the habbit now.
JockVSJock
02-02-2003, 06:12 PM
Originally posted by PolteRGeisT
comment on your declaration of main():
\
For the sake of portability, you should declare the return type of main(). the new C99 standard states that you MUST do this, so you will see future compilers refusing to compile code without return types specifically stated. I suggest you get in the habbit now.
Uh, your going to have to give me more info. on this, cause I have no idea what you are talking about?
bwkaz
02-02-2003, 08:38 PM
According to the C99 standard, you can't say main(). You have to say int main() instead.
And you can't use void main(), either, at least not in gcc 3.1.1 and newer (possibly older versions as well). It worked in gcc 2.95.3, but that's ancient now.
JockVSJock
02-03-2003, 12:38 AM
Thanks everyone for the help. I got it to run the way I wanted to, but for me to fully understand it, I must ask this question:
Why do I have to set this up at the start of the WHILE loop and setup to not equal
while (fGallons != -1 || fMilesDriven != -1) {
and then when I am having input for the following variables such as fGallons and fMilesDriven, why do I say equal to -1
if(fGallons == -1){
break;
}
if(fMilesDriven == -1){
break;
}
Either way, it does work, but the code makes no sense to me. I'm saying one thing and then changing it to say something else.
bwkaz
02-03-2003, 12:46 AM
Do something like:
#include <stdio.h>
int main()
{
float fGallons = 0;
float fMilesDriven = 0;
float fOutput = 0;
while (1) {
printf("Enter -1 to quit anytime \n");
printf("Enter the gallons of gas used \n");
scanf("%f", &fGallons);
if(fGallons < 0) {
break;
}
printf("Enter the miles driven \n");
scanf("%f", &fMilesDriven);
if(fMilesDriven < 0) {
break;
}
fOutput = fMilesDriven / fGallons;
printf("The miles / gallons used was %f \n", fOutput);
}
return 0;
} and that will keep prompting and printing results until you enter -1 (or, in fact, anything less than 0).
JockVSJock
02-03-2003, 08:59 AM
See my last post.
WHY does it work?
I NEED to understand WHY it works, not that it works.
Someone316
02-03-2003, 11:23 AM
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
float fGallons = 0;
float fMilesDriven = 0;
float fOutput = 0;
bool flag =true; //setup for while loop
while (flag) //same as while(flag == true) or while(flag > 0)
//this is an idiom you'll come across in C/C++
//get yourself familiar with this idiom
//remember 0 equals off/false and 1 equals on/true
{
printf("Enter -1 to quit anytime \n");
printf("Enter the gallons of gas used \n");
scanf("%f", &fGallons);
if(fGallons == -1) //if Gallons equal negative one
{
flag =false; //set flag to false which will cause while loop to
//stop. Also it's really not needed because of the break
//command.
break; //break out of the while loop
}
printf("Enter the miles driven \n");
scanf("%f", &fMilesDriven);
if(fMilesDriven == -1) //if Miles Driven is equal to negative one
{
flag =false; //sets flag to false and will exit out of loop
//Again flag =false is not needed, but I do this to reset the
//flag
break; //exit out of while loop
}
fOutput = fMilesDriven / fGallons;
printf("The miles / gallons used was %f \n", fOutput);
}
return 0;
}
FYI when a break command is used inside a loop it does exactly what the name implies. It breaks out of the loop. In your case the while loop will stop once the user enters a negative one. I've made some comments on your code.
JockVSJock
02-03-2003, 11:18 PM
Here is my code. It works, but I really want to understand why. I didn't use and flags, the book that I am going from doesn't use these yet, so I don't want to put the cart in front of the horse here.
Still confused on the 1st While Statement and the IF statements, aren't they saying the exact same thing?
#include <stdio.h>
main()
{
float fGallons = 0;
float fMilesDriven = 0;
float fOutput = 0;
while (fGallons != -1 || fMilesDriven != -1) {
printf("Enter -1 to quit anytime \n");
printf("Enter the gallons of gas used \n");
scanf("%f", &fGallons);
if(fGallons == -1){
break;
}
printf("Enter the miles driven \n");
scanf("%f", &fMilesDriven);
if(fMilesDriven == -1){
break;
}
fOutput = fMilesDriven / fGallons;
printf("The miles / gallons used was %f \n", fOutput);
}
return 0;
}
EscapeCharacter
02-04-2003, 12:06 AM
forget about flags its just anyother varible if you look to the top of his post he sets it equal to true(i dont even think that bool is a type in c...) or 1, your can accomplish the same thing by just putting 1 in place of flag, and yes the while statement you have and the ifs do the same thing thats why everyone said to change the while part to while(1) and let the if statements do the checking for you
Someone316
02-04-2003, 07:53 AM
i dont even think that bool is a type in c...
The stdbool.h allows the user to make use of the bool data type in C :)