Thread: loop debug

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    loop debug

    Hi, I am a c++ beginner. I have written this code below. The compiler recognizes it and compiled it. But once the program gets into the loop, I can't get out of the loop even by meeting the conditions. I checked the syntax of the code and I don't think there is anything wrong with it. But I don't know what's wrong with it. Please help. I gratefully appreciate it. Thank you!

    Code:
    //*************************************************************
    // Mileage Program
    // This program computes the miles per gallon a car gets on a
    // trip
    //*************************************************************
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	int fillupFlag;		// Flag variable to exit the fillup loop
    	float startingMileage;	// Starting mileage
    	float endingMileage;	// Ending mileage
    	float fillup;		// amount(s) in gallons of fillups
    	float milesPerGallon;	// miles per gallon the car gets on the trip
    	float totalFillup;	// sum of the amount of fillups	
    
    	totalFillup = 0;	// Total amount of fillup in the beginning
    	fillupFlag = 1;		// Initializing the flag variable
    		// not equal to 0 so that the program does not skip the loop
    	
    
    	cout << "Welcome to the Mileage Program!" << endl;
    	cout << "This program computes the miles per gallon a car gets "
    	     << "on a trip" << endl;
    	cout << " " << endl;
    	cout << "Please enter the starting mileage: " ;
    	cin >> startingMileage;
    	cout << "Please enter the ending mileage: " ;
    	cin >> endingMileage;
    
    	while (fillupFlag != 0)
    	{
    		cout << "Fillup #" << fillupFlag << endl;
    		cout << "Please enter the amount of the fillup in gallons"
    		     << endl;
    		cout << "(Enter 0 when there is no more fillup): ";
    		cin >> fillup;
    		totalFillup = totalFillup + fillup; // Calculating total
    					 // amount in gallons of fillups
    		if (fillup = 0.0)		// Are there anymore fillups?
    			fillupFlag = 0;		// No.
    		else
    			fillupFlag++;	// Yes.
    	}
    
    	milesPerGallon = (endingMileage - startingMileage) / totalFillup;
    
    	cout << "The miles per gallon your car gets on the trip is"
    	     <<" approximately " << milesPerGallon << " miles per gallon." 
    	     << endl;		// Outputing the results
    	
    	return 0;
    }

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    try this
    Code:
    if (fillup == 0.0)
    insteed
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Thank you very much for the pointer. The program worked after the correction!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM