Infinite Loop Help!
Hi. I don't know why why program doesn't work the way I want to.It keeps looking whenever I want to type '-1'. I know I', experiencing an infinite loop but I'm not sure how to fix it.
// exercise 3
using namespace std;
T1miles = 0;
Tgallous = 0;
counter = 0;
while (counter >= 0)
cout << "Enter miles driven(-1 to quit): "<< endl;cin>>miles;
cout<<"Enter gallons used: "<<endl;cin>>gallous;
MPG = miles/gallous;
cout << "MPG this trip: "<<MPG<<endl;
T1miles = T1miles + miles;
Tgallous = Tgallous + gallous;
T2miles = T1miles/Tgallous;
cout<<"Total MPG: "<<T2miles;
counter = counter + 1;}
Erase lines from your code until you isolate the issue
sorry i don't really understand what you mean, example?
You haven't set the condition for the program to quit when you input -1.
When do you want it to allow "-1" to be the option to quit? Only for when they're entering the miles?
I think this will work:
If you put that after
, I believe it will work.
the counter shouldn't be the condition
miles is the condition
you aren't even inputting into the counter
and it doesn't test the condition again until the end of the loop
while (miles != -1 )
Topic archived. No new replies allowed.