Thread: Easy While Loop Question.

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Easy While Loop Question.

    I am trying to get the user to keep inputing something until they enter a sentinel value here is an example of what I want to do:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int MilesDriven;
        float GallonsUsed;
        int TotalGallons;
        
        int TotalMiles;
        
        float MPG;
        int TotalMPG;
        
        while (GallonsUsed != -1)
        {
              TotalGallons = GallonsUsed + GallonsUsed;
              TotalMiles = MilesDriven + MilesDriven;
              TotalMPG = MPG + MPG;
              
              cout <<"How many gallons used : ";
              cin >>GallonsUsed;
              
              cout <<"How many miles driven : ";
              cin >>MilesDriven;
              
              MPG = MilesDriven / GallonsUsed;
              
              cout <<"The miles/gallon for this tank was " <<MPG;
              cout <<endl;
        }
          cout <<"The average miles/gallon was " <<TotalMPG <<endl;
          system("PAUSE");     
          return 0;
    }
    It works fine but if the user inputs -1 it continues with the rest of the loop before exiting. I need it to leave the while loop when the user enters -1 for GallonsUsed.
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int MilesDriven;
        float GallonsUsed;
        int TotalGallons;
        
        int TotalMiles;
        
        float MPG;
        int TotalMPG;
        
        while (GallonsUsed != -1)
        {
              TotalGallons = GallonsUsed + GallonsUsed;
              TotalMiles = MilesDriven + MilesDriven;
              TotalMPG = MPG + MPG;
              
              cout <<"How many gallons used : ";
              cin >>GallonsUsed;
              
    		  if (GallonsUsed != -1)
    		  {
    
    			cout <<"How many miles driven : ";
    			 cin >>MilesDriven;
              
    			MPG = MilesDriven / GallonsUsed;
              
    			cout <<"The miles/gallon for this tank was " <<MPG;
    			cout <<endl;
    		  }
    
    		  else cout << endl;
        }
          cout <<"The average miles/gallon was " <<TotalMPG <<endl;
          system("PAUSE");     
          return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  4. Message loop question.
    By antex in forum Windows Programming
    Replies: 6
    Last Post: 04-03-2007, 10:13 AM
  5. While loop question
    By rmathus in forum C Programming
    Replies: 3
    Last Post: 10-27-2003, 06:51 PM