while loop question

This is a discussion on while loop question within the C++ Programming forums, part of the General Programming Boards category; hello there all, i am just beginning to learn the C++ language and i am having trouble with this excercise ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    3

    while loop question

    hello there all, i am just beginning to learn the C++ language and i am having trouble with this excercise from the book that i am doing:

    Code:
    
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
       int totalMile, totalGallon, gallonCounter, aveGallon, aveMile;
       double milePerGallon, average, mile, gallon, mileGal;
       totalMile = 0;
       totalGallon = 0;
       gallonCounter = 0;
       
       cout << "Enter the gallons used, (-1 to end) : " << endl;
       cin >> gallon;
       cout << "Enter the miles driven: " << endl;
       cin >> mile;
       mileGal =  mile / gallon;
       cout << "The miles / gallon for this tank was: " << setprecision( 2 )
    		    << setiosflags( ios::fixed | ios::showpoint )
    		    << mileGal << endl;
    
       while ( gallon != -1)
       {
       	totalGallon = totalGallon + gallon;
    	totalMile = totalMile + mile;
    	gallonCounter = gallonCounter + 1;
    	cout << "Enter the gallons used, (-1 to end) : " << endl;
       	cin >> gallon;
       	cout << "Enter the miles driven: " << endl;
       	cin >> mile;
       	mileGal =  mile / gallon;
       	cout << "The miles / gallon for this tank was: " << setprecision( 2 )
    		<< setiosflags( ios::fixed | ios::showpoint )
    	        << mileGal << endl;
       }
    
       
       if ( gallonCounter != 0 )
       {
    	aveMile = totalMile/gallonCounter;
    	aveGallon = totalGallon/gallonCounter;
       	average = static_cast< double >( aveMile )/static_cast< double >( aveGallon );
    	cout << "The overall average miles/gallon was: " << setprecision( 2 )
    		    << setiosflags( ios::fixed | ios::showpoint )
    		    << average << endl;
       }
       
       else
       {
       	cout << "No gallons entered." << endl;
       }
       
       return 0;
    }
    the code is executing but once i want to end the entering of data through -1 it adds the -1 to the computation.

    i just want to know what is wrong with my code.

    thank you very much.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    The basic principle is...store the input to "gallon"; check that "gallon" not equal -1

    Code:
    cout << "Enter the gallons used, (-1 to end) : " << endl;
    cin >> gallon;
    while ( gallon != -1) {
    // do processing
    // ...
      cout << "Enter the gallons used, (-1 to end) : " << endl;
      cin >> gallon;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    pardon me sir but could you elaborate it coz i am just a newbee in C++, will i reorder my code? or coz i also have that mile input, the excercise in the book wants an output like this:

    Code:
    Enter the gallon used ( -1 to end ): 12.8
    Enter the mile driven: 287
    The miles/gallon for this tank was 22.42
    
    Enter the gallon used ( -1 to end ): 10.3
    Enter the mile driven: 200
    The miles/gallon for this tank was 19.41
    
    Enter the gallon used ( -1 to end ): 5
    Enter the mile driven: 120
    The miles/gallon for this tank was 24.00
    
    Enter the gallon used ( -1 to end ): -1
    
    The overall average miles/gallon was 21.60

    thanks a lot for the reply.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    When you read a value, test it straight away. For example;
    Code:
      cin >> gallon;
      if (gallon != -1)
      {
          //  do whatever you want with gallon
      }
    In addition, I also suggest checking that gallon is non-zero; dividing by zero is rarely a good idea ....

  5. #5
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You also need to get into the habit of checking your streams for errors. For instance, if someone fails to enter a value that is convertable to a float, the stream goes into an error state and any subsequent reads will fail. I find it best to read in all user data (and this includes data from file, socket, etc.) into char arrays (I prefer strings when in C++), then do the processing on the string. The input can still fail (the stream could be broken, for instance), but you have much better control over what is going on and I feel it results in better to maintain code.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    thank you very much all.

    it helps me alot.

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. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 12:08 PM
  5. while loop question
    By rayrayj52 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21