Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function: void introduction(istream &is, ostream &os, string target, str

ID: 3677027 • Letter: W

Question

Write a function: void introduction(istream &is, ostream &os, string target, string replacement); That provides instructions for the user. Note that a user who has played the game previously may not wish to see the instructions, and so should be allowed to decline the opportunity to see them. The function should read text from the input stream is and write the instructions to the output stream os. In the course of writing text to the output stream, any occurrences of the string specified by the target parameter are to be replaced with the string specified by the replacement parameter. For your testing, to create a set of instructions like those shown in the sample run, you can use the file input.txt and set the target to "$SPACECRAFT" and the replacement to "APOLLO".
The input.txt file contents are as follows:
LUNAR LANDER INSTRUCTIONS
You are landing on the moon and have taken over manual control 1000 feet above a good landing spot. You have a downward velocity of 50 feet/sec. 150 units of fuel remain. Here are the rules that govern your $SPACECRAFT space-craft:
(1) After each second, the height, velocity, and remaining fuel will be reported via DIGBY, your on-board computer.
(2) After each report, enter the number of units of fuel you wish to burn during the next second. Each unit of fuel will slow your descent by 1 foot/sec.
(3) The maximum thrust of your engine is 30 feet/sec/sec or 0 units of fuel per second.
(4) When you contact the lunar surface, your descent engine will automatically shut down and you will be given a report of your landing speed and remaining fuel.
(5) If you run out of fuel, you will no longer be prompted to enter the number of units to burn each second. Your second by second reports will continue until you contact the lunar surface.

Explanation / Answer

Source.cpp
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void introduction(istream &is, ostream &os, string target, string replacement) {

   bool quit = false;
   string cur_line = "";

   while (getline(is, cur_line)) {

       //parse out target
       int pos = cur_line.find(target);
       while (pos != string::npos) {
           cur_line = cur_line.replace(pos, target.length(), replacement);
           pos = cur_line.find(target);
       }

       os << cur_line << " ";
   }
}

void updateStatus(double &velocity, double burnAmount, double &fuelRemaining, double &height) {
   double oldvelocity = velocity;
   velocity = velocity + 5 - burnAmount;
   height = height - (oldvelocity + velocity) / 2;
   fuelRemaining -= burnAmount;
}

//parameters are post crash
void touchdown(double &elapsedTime, double &velocity, double &burnAmount, double &height) {
   double oldvelocity = velocity - 5 + burnAmount;
   double oldheight = height + (oldvelocity + velocity) / 2;
   elapsedTime -= 1;
   double delta = (sqrt(pow(oldheight, 2) + oldheight*(10 - 2 * burnAmount)) - oldvelocity) / (5 - burnAmount);
   elapsedTime += delta;
   velocity = oldvelocity + (5 - burnAmount) * delta;

}

void damage(ostream &os, double velocity) {
   if (velocity <= 0) os << "Congratulations! A perfect landing!! Your license will be renewed...later.";
   else if (velocity < 2) os << "A little bumpy";
   else if (velocity < 5) os << "You blew it!!!!!! Your family will be notified...by post.";
   else if (velocity < 10) os << "Your ship is a heap of junk !!!!! Your family will be notified...by post.";
   else if (velocity < 30) os << "You blasted a huge crater !!!!! Your family will be notified...by post.";
   else if (velocity < 50) os << "Your ship is a wreck !!!!! Your family will be notified...by post.";
   else os << "You totaled an entire mountain !!!!! Your family will be notified...by post.";
}

void main() {

   ifstream input("input.txt");
   filebuf fb;
   fb.open("output.txt", ios::out);
   ostream output(&fb);

   string target = "$SPACECRAFT";
   string replacement = "APOLLO";

   string readintro;
   cout << "Read intro? ";
   getline(cin, readintro);
   if (readintro[0] == 'y' || readintro[0] == 'Y')
       introduction(input, cout, target, replacement);
   cout << " ";

   double altitude = 0;
   double velocity = 0;
   //double const ACCELERATION = -1.63;
   double time = 0;
   double fuel;
   double fuel_supply;

   //prompt user for game start
   cout << "What is the initial altitude? ";
   cin >> altitude;
   cout << "What is the initial velocity? ";
   cin >> velocity;
   cout << "What is the initial fuel supply? ";
   cin >> fuel_supply;

   do {

       //print altitude
       cout << "After " << time << " seconds, the spaceship is " <<
           altitude << " meters high, and " << velocity << " meters/second fast" << endl;

       cout << "You have " << fuel_supply << " gallons of fuel left... " << endl;

       fuel = 0;
       if (fuel_supply > 0) {

           cout << "How much fuel to burn? ";
           cin >> fuel;

           if (fuel_supply-fuel > fuel_supply || fuel_supply-fuel < 0) {
               do {
                   cout << "You don't have that much fuel!" << endl;
                   cout << "How much fuel to burn? ";
                   cin >> fuel;
               } while (fuel_supply-fuel < 0 || fuel_supply-fuel > fuel_supply);

           }
       }

       updateStatus(velocity, fuel, fuel_supply, altitude);
       time += 1;

   } while (altitude > 0);

   cout << " ";
   touchdown(time, velocity, fuel, altitude);
   damage(cout, velocity);
   cout << " ";
}


input.txt
LUNAR LANDER INSTRUCTIONS

You are landing on the moon and have taken over manual
control 1000 feet above a good landing spot. You have a
downward velocity of 50 feet/sec. 150 units of fuel remain.

Here are the rules that govern your $SPACECRAFT space-craft:
(1) After each second, the height, velocity, and remaining
     fuel will be reported via DIGBY, your on-board computer.
(2) After each report, enter the number of units of fuel you
     wish to burn during the next second. Each unit of fuel
     will slow your descent by 1 foot/sec.
(3) The maximum thrust of your engine is 30 feet/sec/sec or
     0 units of fuel per second.
(4) When you contact the lunar surface, your descent engine
     will automatically shut down and you will be given a
     report of your landing speed and remaining fuel.
(5) If you run out of fuel, you will no longer be prompted
     to enter the number of units to burn each second. Your
     second by second reports will continue until you contact
     the lunar surface.

sample output

Read intro? input.txt                                                                                                                                       
                                                                                                                                                            
                                                                                                                                                            
What is the initial altitude? 100                                                                                                                           
What is the initial velocity? 50                                                                                                                            
What is the initial fuel supply? 100                                                                                                                        
After 0 seconds, the spaceship is 100 meters high, and 50 meters/second fast                                                                                
You have 100 gallons of fuel left...                                                                                                                        
How much fuel to burn? 50                                                                                                                                   
After 1 seconds, the spaceship is 72.5 meters high, and 5 meters/second fast                                                                                
You have 50 gallons of fuel left...                                                                                                                         
How much fuel to burn? 50                                                                                                                                   
After 2 seconds, the spaceship is 90 meters high, and -40 meters/second fast                                                                                
How much fuel to burn? 50                                                                                                                                   
After 1 seconds, the spaceship is 72.5 meters high, and 5 meters/second fast                                                                                
You have 50 gallons of fuel left...                                                                                                                         
How much fuel to burn? 50                                                                                                                                   
After 2 seconds, the spaceship is 90 meters high, and -40 meters/second fast                                                                                
You have 0 gallons of fuel left...                                                                                                                          
After 3 seconds, the spaceship is 127.5 meters high, and -35 meters/second fast                                                                             
You have 0 gallons of fuel left...                                                                                                                          
After 4 seconds, the spaceship is 160 meters high, and -30 meters/second fast                                                                               
You have 0 gallons of fuel left...                                                                                                                          
After 5 seconds, the spaceship is 187.5 meters high, and -25 meters/second fast                                                                             
You have 0 gallons of fuel left...                                                                                                                          
After 6 seconds, the spaceship is 210 meters high, and -20 meters/second fast                                                                               
You have 0 gallons of fuel left...                                                                                                                          
After 7 seconds, the spaceship is 227.5 meters high, and -15 meters/second fast                                                                             
You have 0 gallons of fuel left...                                                                                                                          
You have 0 gallons of fuel left...                                                                                                                          
After 8 seconds, the spaceship is 240 meters high, and -10 meters/second fast                                                                               
You have 0 gallons of fuel left...                                                                                                                          
After 9 seconds, the spaceship is 247.5 meters high, and -5 meters/second fast                                                                              
You have 0 gallons of fuel left...                                                                                                                          
After 10 seconds, the spaceship is 250 meters high, and 0 meters/second fast                                                                                
You have 0 gallons of fuel left...                                                                                                                          
After 11 seconds, the spaceship is 247.5 meters high, and 5 meters/second fast                                                                              
You have 0 gallons of fuel left...                                                                                                                          
After 12 seconds, the spaceship is 240 meters high, and 10 meters/second fast                                                                               
You have 0 gallons of fuel left...                                                                                                                          
After 13 seconds, the spaceship is 227.5 meters high, and 15 meters/second fast                                                                             
You have 0 gallons of fuel left...                                                                                                                          
After 14 seconds, the spaceship is 210 meters high, and 20 meters/second fast                                                                               
You have 0 gallons of fuel left...                                                                                                                          
After 15 seconds, the spaceship is 187.5 meters high, and 25 meters/second fast                                                                             
You have 0 gallons of fuel left...                                                                                                                          
After 15 seconds, the spaceship is 187.5 meters high, and 25 meters/second fast                                                                             
You have 0 gallons of fuel left...                                                                                                                          
After 16 seconds, the spaceship is 160 meters high, and 30 meters/second fast                                                                               
You have 0 gallons of fuel left...                                                                                                                          
After 17 seconds, the spaceship is 127.5 meters high, and 35 meters/second fast                                                                             
You have 0 gallons of fuel left...                                                                                                                          
After 18 seconds, the spaceship is 90 meters high, and 40 meters/second fast                                                                                
You have 0 gallons of fuel left...                                                                                                                          
After 19 seconds, the spaceship is 47.5 meters high, and 45 meters/second fast                                                                              
You have 0 gallons of fuel left...                                                                                                                          
                                                                                                                                                            
                                                                                                                                                            
You totaled an entire mountain !!!!! Your family will be notified...by post.                                                                                

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote