I need help making a helicopter simulator program in C++. The instructions are:
ID: 3671044 • Letter: I
Question
I need help making a helicopter simulator program in C++. The instructions are:
Keywords: Overload, method, constructor/destructor, class, struct, pointer, dynamic memory allocation, string manipulation, vector, read/write file
Implement a helicopter simulator:
1) create a class helicopter containing the public method API; API calls the private methods: weather_input, fly_helicopter; (2 points)
2) The local pointer variable *myHelicopter defined as “struct flying_data” declared in the method API; myHelicopter requires struct dynamic allocation to match what user informs about how many helicopters are flying: 2 or 3; This struct contains: pilot_name, rockets, visibility; declare rockets as a type of variable “vector” of integer containing 4 rockets, 1 is loaded or 0 is empty/discharged. (3 points)
3) Method weather_input: read from a file what is the current visibility – if below 60% abort for safety reasons; also ask how many bars of fuel the helicopter has: 10 bars meaning 100%, 5 bars is 50% and so on; if fuel has only 10% of fuel it is not safe to fly.(2 points)
4) Use pointers to pass myHelicopter among the functions that are called from API. From fly_helicopter: print into a file on every loop iteration the fuel level; the name of the file for the output should be the pilot name; every loop decreases 3% from current tank level; (3 points)
This car program is similar to what the program is trying to achieve.
#include<string>
#include<iostream>
using namespace std;
class Car {
public:
Car();
~Car();
void API();
struct data_car {
string vehicle_name = "";
int terrain = 0;
double speed = 0.0;
double tank_level = 0.0;
}myCar;
private:
void simulator_ch(data_car*);
void run_simulator(data_car*);
};
Car::Car()
{
API();
}
Car::~Car()
{
}
void Car::API()
{
simulator_ch(&myCar);
run_simulator(&myCar);
}
void Car::simulator_ch(data_car* myCar)
{
cout << "How TOUGH is the terrain?? (1:easy, 2:tough, 3:really hard)";
while (myCar->terrain != 1 && myCar->terrain != 2 && myCar->terrain != 3) {
try {
cin >> myCar->terrain;
}
catch (exception e)
{
cout << "Invalid terrain!" << endl;
}
if (myCar->terrain != 1 && myCar->terrain != 2 && myCar->terrain != 3) {
cout << "Please enter a valid terrain toughness";
}
}
cout << "Select the average speed to drive (1: slow, 2: medium, 3: fast)";
int ch;
while (ch != 1 && ch != 2 && ch != 3) {
try {
cin >> ch;
}
catch (exception e) {
cout << "Invalid speed!" << endl;
}
if (ch != 1 && ch != 2 && ch != 3) {
cout << "That's not an allowed speed setting, try another! ";
}
}
switch (ch) {
case 1:
myCar->speed = 0.3;
break;
case 2:
myCar->speed = 0.6;
break;
case 3:
myCar->speed = 0.8;
break;
}
cout << "Select tank level for the fuel when you start to drive (1: 100%, 2. 80%, 3. 50%) ";
int i;
while (i != 1 && i != 2 && i != 3) {
try {
cin >> i;
}
catch (exception e) {
cout << "Invalid fuel amount" << endl;
}
if (i != 1 && i != 2 && i != 3) {
cout << "Please enter a valid fuel amount";
}
}
switch (i) {
case 1:
myCar->tank_level = 100.0;
break;
case 2:
myCar->tank_level = 80.0;
break;
case 3:
myCar->tank_level = 50.0;
break;
}
}
void Car::run_simulator(data_car* myCar)
{
while (myCar->tank_level > 0.1) {
cout << "New tank level: " << myCar->tank_level << endl;
myCar->tank_level = myCar->tank_level - (myCar->terrain*myCar->speed );
}
}
int main() {
Car car;
return 0;
}
Explanation / Answer
Comments added
#include<string>
#include<iostream>
#include <fstream>
#include <sstream>
using namespace std;
class Helicopter {
public:
Helicopter();
~Helicopter();
void API();
//structure declared
struct flying_data {
string pilot_name = "";
vector<int> rockets = 0;
int visibility = 0.0;
double tank_level = 0.0;
}myHelicopter;
//method protoypes declaration
private:
void weather_input(flying_data*);
void fly_helicopter(flying_data*);
};
//constructor
Helicopter::Helicopter()
{
API();
}
//empty constructor
Helicopter::~Helicopter()
{
}
//calling simulation methods
void Helicopter::API()
{
weather_input(&myHelicopter);
fly_helicopter(&myHelicopter);
}
void Helicopter::weather_input(flying_data* myHelicopter)
{
//reading file
std::ifstream readfile("visibility.txt");//reding numbers from text file
std::string eachline;
while (std::getline(readfile, eachline))
{
std::istringstream iss(eachline);
int n;
while (iss >> n)//reading visibility value from file
{
myHelicopter->visibility = n;
}
}
//if visibility greater than 60
if(myHelicopter->visibility > 60) {
//reading tank level from pilot
cout<<" How many fuel bars are there in helicopter: ";
duoble fuel;
cin>>fuel;
myHelicopter->tank_level = fuel;
//if low fuel
if(fuel == 1){ //that is 10%
cout<<" Low fuel.. so it is not safe to fly.";
}
else{ //if fuel is enough to fly
cout<<" conditions are good to fly.";
}
}
//if low visibility that is less than 60
else{
cout<<" Conditions are not good to fly helicopter.";
}
}
//simulating helicopter
void Helicopter::fly_helicopter(flying_data* myHelicopter)
{
//outputting to file
string fileName = myHelicopter->pilot_name +".txt";
ofstream out_data(fileName);
//until tank level greater than 0.1
while (myHelicopter->tank_level > 0.1) {
out_data << " New tank level: " << myHelicopter->tank_level;
//decreasing 3% every time
myHelicopter->tank_level = myHelicopter->tank_level - 0.3;
}
}
int main() {
//creating helicopter object
Helicopter Helicopter;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.