Need help with my program

This is a discussion on Need help with my program within the C++ Programming forums, part of the General Programming Boards category; Hey guys I'm new to this board, but if you guys could share a little insight I would appreciate it. ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    2

    Need help with my program

    Hey guys I'm new to this board, but if you guys could share a little insight I would appreciate it. Now the program is for figuring fuel mileage.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    #include <iomanip>
    
    
    
     // main function begins
    int main()
    { 
    
     
    	double gal; //gallons used
    	int miles; //miles driven
    	double mpg; //mpg
    	int counter;//counter used
    	double total; //total of miles
        double average; //overall average
    
    	total = 0;
    	counter = 0;
    
    
    	cout << "Enter the gallons used (-1 to end): ";
    	cin >> gal;
    
    	cout << "Enter the miles driven ";
    	cin >> miles;
    
    	mpg = miles / gal;
    		cout << "The miles / gallon for this tank was " << mpg << endl;
    
    		while ( gal != -1 ) {
    		total = total + gal;
    		counter = counter + 1;
    
    		cout << "Enter the gallons used (-1 to end) "; 
    		cin >> gal;
    		cout << "Enter the miles driven ";
    		cin >> miles;
    		
    		mpg = miles / gal;
    		cout << "The miles / gallon for this tank was " << mpg << endl;
    
    		
    		}
    		if ( gal != 0 ) {
    			average = ( double) total  / counter;
    
    			cout << "The overall average miles/gallon was " << average <<endl;
    		}
    		
    		
    		else
    			cout << " No data was entered" << endl;
    		
    				
    		return 0;
    }


    Now what happens is, I can input the gallons used and miles travedeled over and over, and it will give me the the miles/gallon answer which is correct. But as soon as I hit -1 to end the program it will continue asking for the gal's used and miles driven. It should terminate right after I hit -1. What have I done wrong? Is it my while statement that is incorrect? I've tried several different ways and nothing works. The program has no errors at all, so it must be a incorrect operator or misplacement.

    Thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Hey guys I'm new to this board
    Unfortunately, your first post has ended up in the wrong forum I'll move it to the C++ forum. Welcome aboard...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    2
    Thanks man...yeah I went to the wrong forum....DOH

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    The problem is that while loops don't check until the very end, thus everything between when you enter -1 and the bracket will still run. You need to do one of two things:

    Code:
    while (gal!=-1
    {
       cout << "Enter the gallons used (-1 to end) "; 
       cin >> gal;
       if (gal==-1)
           break;
       etc
    or do something like this:

    Code:
    // ask for gallons
    while (gal!=-1)
    {
         // get miles then print mpg
         // ask for gallons
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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