mardi 3 mars 2015

Having trouble with yes and no loops


So in my computer science class we got to make a lunar lander game. At the very end of the code we are supposed to ask if the user wants to run the game again, while Ive established a yes and no loop, it still doesnt work. It lets me type 'Y' and 'N' but it just breaks right after words.



// Include Section
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

// Main Program
int main( )
{
// Variable Declarations
double E; // Engine output
double V; // Velocity output
double A; // Altitude output
double T; // Time output
double F; // Fuel consumption
double Th; // Thrust
double x; // The variable output the user puts in
double G; // The gravity of the moon
double Acceleration;// The acceleration found between the force and the


gravity double M; // Total Mass of the lander double Force; // The force opposing the lunar gravity double W; // Weight double Thrust; // The initial thrust value char Ans;



A=2300;
V=0;
T=0;
F=300;
G=-1.62;
Thrust=43900;
M= 6831;


// Output Identification
system("CLS");

cout << "Welcome to the Lunar Lander simulation!" << endl;
cout << endl;
cout << "You are in command of the Lunar Lander on its final approach to the moon's surface."
" You need to control the thrust to the main decent engine."
" You will adjust it each second by typing in the percent thurst that you would like for the next second."
" Your goal is to end land the spacecraft at a velocity of less than 1 meter/second."
<<endl;
cout << endl;
cout << "GOODLUCK!"<<endl;
cout << endl;
cout << setw(8) << "Sec" << setw(10) << "Alt" << setw(10) << "Speed" << setw(8) << "Fuel" << endl;

while(F > 0)
{
if(A > 0)
{
cout << setw(8) << T << setw(10) << A << setw(10) << V
<< setw(8) << F << setw(14) << " Enter percent burn (0-100):";
cin >> x;
if( x < 0|x > 100)
{
cout << "Please pick a valid percentage."<< endl;
}
else
{
T++;
x=x/100;
Th= x*Thrust;
E= 13.6*x;
F= F-E;
M =M-E;
W=M*G;
Force=Th+W;
Acceleration=Force/M;
V=V+Acceleration*1;
A=A+V*1-.5*Acceleration*1;
}
}
else
{
if(V <= -1 & V >=-3)
{
cout << "Congratulations!" << endl;
cout << "Would you like to run the simulation again? <Y/N>"<<endl;
cin >> Ans;
if(Ans == 'Y'|| Ans == 'y')
return 0;
else
break;
}
else
{
cout << "Game Over" <<endl;
cout<< "Would you like to run the simulation again? <Y/N>"<<endl;
cin >> Ans;
if(Ans == 'Y'||Ans == 'y')
return 0;
else
break;
}
}
if(F<=0)
{
cout<< "Would you like to run the simulation again? <Y/N>"<<endl;
cin >> Ans;
if(Ans == 'Y'|| Ans=='y')
return 0;
else
break;
}
}


}




Aucun commentaire:

Enregistrer un commentaire