Multiple problems with my program

This is a discussion on Multiple problems with my program within the C Programming forums, part of the General Programming Boards category; The problem is towards the end where it prints out the status report, if you type in values which require ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Multiple problems with my program

    The problem is towards the end where it prints out the status report, if you type in values which require it, as the altitude reaches 0, it should stop at 0, but instead it goes beyond which messes up the rest of the calculations. The early outputs are correct though. Also, another problem is how do I put in a loop which asks the user if he or she wants to do it again. Would I do a while loop which starts from the beginning to the end?

    Code:
    int main()
    {
    	
    	int		ship_altitude,
    			ship_fuel_supply,		
    			beam_strength,
    			time;			
    	float		fuel_required;		
    	
    	char		restart;
    
    	printf ("\nEnter the current altitude of the ship: ");
    	scanf ("%d", &ship_altitude);
    
    	printf ("\nEnter the current fuel supply of the ship: ");
    	scanf ("%d", &ship_fuel_supply);
    
    	printf ("\nEnter the strength of the enemy tractor beam: ");
    	scanf ("%d", &beam_strength);
    
    
    	printf ("\n\n\nSimulation:\n\n\n");
    
    	printf ("\n***********************Computer Report on Ship Status************************\n\n\n");
    
    	printf ("The Enterprise has gone into orbit around Delta Tau at an altitude of %d.\n", ship_altitude);		/* Print Altitude */
    
    	printf ("It has a fuel supply of %d Kg. of fuel.\n", ship_fuel_supply);										/* Print Fuel Supply */
    
    	printf ("It has been attacked by a tractor beam of strength %d Km/min/min.\n", beam_strength);				/* Print Beam Strength */
    
    	fuel_required = ((1 - (double)ship_altitude / 200000) * (double)beam_strength);								/* Formula */
    
    	printf ("At this altitude, %.1f Kg. of fuel are required to escape.\n\n", fuel_required);					/* Print fuel requirement */
    
    	/* Test between having enough fuel and not */
    	if (ship_fuel_supply >= fuel_required)
    		printf ("There is efficient fuel to escape.\n\n\nRecommendation:  Please start the engines and initiate your escape!\n");
    	 
    	else if ( ship_fuel_supply < fuel_required)
    	{
    		printf ("There is not enough fuel to escape.\n\n\nRecommendation:  Please tell Trip to start shoveling!\n\n");
    		printf ("Here is what to expect:\n\n");
    		printf ("Time      Altitude      Fuel Available      Fuel Required\n");
    		printf ("__________________________________________________________\n");
    		
    		time = 0;
    		
            do
                    {
                    time = time + 1;
                    ship_altitude = ship_altitude - beam_strength * time * time;
                    ship_fuel_supply = ship_fuel_supply + 10;
                    fuel_required = ((1 - (double)ship_altitude / 200000) * (double)beam_strength);
    				printf ("%d min.      ", time);
                    printf ("%d KM.           ", ship_altitude);
                    printf ("%d KG.         ", ship_fuel_supply);
                    printf ("%.1f KG.         \n", fuel_required);
                    }
    				while (ship_fuel_supply < fuel_required && ship_altitude >= 0);
    
    				if (ship_altitude == 0)
    				{
    					printf ("In %d minutes, the ship will crash!\n", time);
    					printf ("I recommend sending a farewell message to Star Fleet!");
    				}
    				else if (ship_fuel_supply > fuel_required)
    				{
    					printf ("In %d minutes, it will be safe to escape.\n", time);
    					printf ("I recommend turning on the engines by then.\n");
    				}
    	}
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    Your indenting is horrible, and probably causing you to misinterpret what your program is doing. The compiler uses locations of {}, etc to work out what code is grouped with what. Your indenting does not correspond to the code so, if you are using indenting as a means of understanding program logic, you are confusing yourself.


  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Umm...I only see a couple lines with odd indenting, and even then its still pretty readable...As for the altitude problem, is there any guarentee that it will ever equal exacly zero? What if it goes form say....10 to -3 or something, without stopping at zero on the way? If it does this your if clause will never be tripped, and the countdown will never stop.
    Try changing the line to :
    Code:
    if (ship_altitude <= 0)
    As for doing the program again, then yes, you're on the right track. But I would recomend getting using an integer for restart instead of a character. That way you can initially set it to one, and have a
    Code:
    while (restart)
    And then you can ask the user if he wants to continue at the end, and if he inputs anything other than a 'y' or a 'Y' change the variable to 0 so that the program won't loop.
    Hope this helps
    -Crazed

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    2
    Quote Originally Posted by grumpy
    Your indenting is horrible, and probably causing you to misinterpret what your program is doing. The compiler uses locations of {}, etc to work out what code is grouped with what. Your indenting does not correspond to the code so, if you are using indenting as a means of understanding program logic, you are confusing yourself.
    I'm not sure exactly what you mean, I'm pretty new at this. This is my first program using loops.

    Quote Originally Posted by CrazedBrit
    Umm...I only see a couple lines with odd indenting, and even then its still pretty readable...As for the altitude problem, is there any guarentee that it will ever equal exacly zero? What if it goes form say....10 to -3 or something, without stopping at zero on the way? If it does this your if clause will never be tripped, and the countdown will never stop.
    Try changing the line to :
    Code:
    if (ship_altitude <= 0)
    As for doing the program again, then yes, you're on the right track. But I would recomend getting using an integer for restart instead of a character. That way you can initially set it to one, and have a
    Code:
    while (restart)
    And then you can ask the user if he wants to continue at the end, and if he inputs anything other than a 'y' or a 'Y' change the variable to 0 so that the program won't loop.
    Hope this helps
    -Crazed
    Well, the simulation shows a ship being pulled to a planet, and with each altitude and beam strength you need a certain amount of fuel to successfully escape. But, if there is not enough fuel, and there is not enough altitude to meet the re-fueling, the ship will crash, and the altitude should be 0. But, it is outputting something in the negatives. And as for the loop, while (restart) do I just stick that in the end? Like I said, I'm new at this. Sorry if I sound very stupid.
    Last edited by Seirei; 02-17-2006 at 04:01 PM.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Oh okay, I'm at my home computer now so I could compile it and see what you meant. I understand the problem now.
    Try this:
    Code:
     do
            {
                    time = time + 1;
                    ship_altitude = ship_altitude - beam_strength * time * time;
                    ship_fuel_supply = ship_fuel_supply + 10;
                    fuel_required = ((1 - (double)ship_altitude / 200000) * (double)beam_strength);
    		if (ship_altitude>0)
    		{
    			printf ("%d min.      ", time);
                    	printf ("%d KM.           ", ship_altitude);
                    	printf ("%d KG.         ", ship_fuel_supply);
                    	printf ("%.1f KG.         \n", fuel_required);
    		}
             }
    		while (ship_fuel_supply < fuel_required && ship_altitude >= 0);
    That way the data will not be printed in that last revolution of the do while loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  2. Problems in a game program
    By ChronoSquare in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2006, 07:32 AM
  3. having problems with my card program
    By mac025 in forum C Programming
    Replies: 4
    Last Post: 01-31-2006, 03:26 PM
  4. Problems with easy program
    By chuckster in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2004, 11:08 AM
  5. Minute Program output problems
    By Cobalt in forum C++ Programming
    Replies: 12
    Last Post: 10-12-2003, 10:16 AM

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