Third Project, loops!

My TAs aren't helping nor is the professor. I wrote out all the code on paper first like they suggested and started inputting into the compiler. Trying to get results that work, I have no idea what is wrong. Using loops with if thens are not allowed? I created infinite loops and am completely lost.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*	PROGRAM Enterprise Tractor Beam Egression Calculator


    PLATFORM: Windows OS Microsoft Visual C++ Express 2012

	This program will ask the user if a simulation should be run.  Then, it will ask for altitude, fuel available, and the strength
	of the tractor beam.  If there is enough fuel for escape, the program will recommend escape immediately.  If not, it will recommend shutting 
	off the engines and shoveling antimatter into the fuel cells.  The simulation will run every minute until there is enough fuel to escape or until the altitude is 0
	and the ship crashes.  In either case, the program will display a minute by minute analysis of the data showing the result of the simulation.  At the end, it
	will ask to run another simulation.
*/

#include <iostream>
#include <iomanip>
using namespace std;				
	char y;												// initializing yes or no characters for the choice to redo a simulation
	char n; 
	char Y; 
	char N; 
	char choice;
	const int SHOVEL = 10;								// scotty's constant shovel speed
	int altitude;										// initializing my integer values
	int fuel_available;
	int fuel_required;
	int beam_strength;
	int minute;											// the counter for how long a simulation is running

int main ()
{	
	cout << "Welcome to the Enterprise's Tractor Beam Egression Calculator!\n";
	cout << "Would you like to run a simulation? Please enter (y)es or (n)o.\n";
	cin >> choice;
	if ( choice = 'y' || choice = 'Y' )
	{
		cout << "You entered (y)es!\n";  
		cout << "Please enter your altitude: ";
		cin >> altitude;
	}
	else if ( choice = 'n' || choice = 'N' )
	{
		cout << "Program terminating...";
	}
	while ( choice =! y && choice =! Y && choice =! n && choice =! N )				// if yes then run a simulation, if no then notify of termination and quit.
	{
		cout << "Please enter (y)es or (n)o: \n";  
		cin >> choice;
	}
		while (altitude <= 0)
		{
			cout << "ERROR! Enter value greater than zero!\n";	// must be greater than 0, assume ship is flying
			cin >> altitude;
		}
		cout << "Please enter your current fuel available: ";
		cin >> fuel_available;
		while (fuel_available < 0)
		{
			cout << "ERROR! Enter a value greater than zero!\n"; // must be greater than 0, assume ship is flying
			cin >> fuel_available;
		}
		cout << "Please enter the strength of the tractor beam: "; 
		cin >> beam_strength;
		while (beam_strength < 0)
		{
			cout << "ERROR! Enter a positive value: ";			// must be greater than 0, assume tractor beam is being used
			cin >> beam_strength;
		}
		fuel_required = ((1-altitude)/200000) * beam_strength;	// fuel required calculation
		if (fuel_required <= fuel_available)
		{
			cout << "\nMinute Counter\tCurrent Altitude\tCurrent Fuel Supply\tTractor Beam Strength\tFuel Required to Escape\n";
			cout << "minute\taltitude\tfuel_available\tbeam_strength\tfuel_required";
			cout << "Set engine to full throttle and escape!";	// escape if-then
		}
		else 
		{
			cout << "Shut down engines, add antimatter to the fuel cells!\n"; // shut down engines, add fuel
			do
			{
				fuel_available = fuel_available + SHOVEL;			// adding 10 kg to fuel, then adding one minute
				minute++;
				cout << "minute\taltitude\tfuel_available\tbeam_strength\tfuel_required";
			}
			while (fuel_available < fuel_required);
			if (altitude <= 0)
			{
				cout << "The ship has crashed on the planet surface.";	// if alt = 0, crash the ship
			}
			else
			{
				altitude = altitude - beam_strength * minute * minute; // altitude change function if not crashed, AND not escaped
			}
		}
		
	
	return 0;
}
quick glance shows me you're using while for testing the validity of some of your variables:

1
2
3
4
5
while (beam_strength < 0)
		{
			cout << "ERROR! Enter a positive value: ";			// must be greater than 0, assume tractor beam is being used
			cin >> beam_strength;
		}


surely you want to use 'if' here and not 'while'? (and handle the error sensibly).

there's a lot of other stuff, but fix that for now.
Last edited on
That line of code looks correct. He is saying while he has a bad value then keep asking for one until he goods a good value.

What exactly is your problem OP? It looks correct to me.

*edit actually your problem might be that you are doing integer division maybe try double division for your fuel unless they are only integral values ( 1 , 2 , 3... )
Last edited on
Topic archived. No new replies allowed.