Homework 34


You will write a simple program to track ship movement over time. You are provided with the struct Point defined in dbpoint.h and dbpoint.cpp. You can use this point struct to help track a ship’s position.

Your program will read information from the user for a single boat. Specifically, you will be given its name, position (as an (x,y) pair giving its position in nautical miles from some fixed point), its heading (in degrees from north) and its speed in knots. In this scenario, the boat never changes speed or heading once set by the user. Your program will then read a number of hours from the user, update the boat's position, and print a position report after that number of hours. It will keep reading a number of hours and printing a position report over and over until the user indicates that he or she wants to quit by entering a -1. Below is a sample run of the program (user input shown in red). NOTE: several hints are given below!!!!!     

Enter boat info: Slowpoke (140.3,33.7) 37.0 degrees 10 kts
Currently: Slowpoke (140.3,33.7) 37 degrees 10 kts
Time elapsed in hours: 0.5
Currently: Slowpoke (143.309,37.6932) 37 degrees 10 kts
Time elapsed in hours: 2.5
Currently: Slowpoke (158.354,57.6591) 37 degrees 10 kts
Time elapsed in hours: -1

Requirements: You must make a struct Boat that stores all the necessary information about a boat, and make all the work (like reading and writing and moving the boat) into functions. There should be a file Boat.h and Boat.cpp that implements all of this stuff. Your main.cpp should be very simple, and should include Boat.h (but not Boat.cpp). If you do this right, it should be trivial to transform this program so that it would track many boats simultaneously!

Turn In: A screen capture of your program running on the given sample input, and a printout of Boat.h, Boat.cpp and main.cpp.


1.      You will first want to add dbpoint.h and dbpoint.cpp to your Visual Studio project. When making a .h file from scratch, select “C/C++ Header File” instead of “C++ Source File.”

2.      Your boat.h file should have this at the very top:
#ifndef BOAT_H
#define BOAT_H
#include "dbpoint.h"
and this at the very bottom:
See class 35 for details on this.

3.      Remember that a compile error at, say main.cpp line 12, may actually be caused by something in another file that was included.  For instance, did you forget a semicolon somewhere in boat.h? (this can cause obscure errors in main.cpp)

4.      Consider how the following function might help you.  You are free to use it or you may create your own from scratch.

Point move(Point position, double heading, double speed, double time)
  double PI = 3.14159;
  Point delta;         // the displacement of the boat over the specified time
  delta.x = time*speed*sin(heading*PI/180);     // displacement in x direction
  delta.y = time*speed*cos(heading*PI/180);     // displacement in y direction
  return position + delta;   // requires overload of operator+ for struct Point