C++ Question 2
You will read in data about the planets from a text file, and
then print out the data about that planet when the user requests
the data. You will need to create a Planet Class (since this
assignment does not cover templates, you are expected to split your
class into .cpp and .h files), as well as a .cpp/.h library with
the functions described below
Planet Class
For the first phase of the question, you will create a Planet
class.
Include the iostream and fstream libraries in the Planet class
so we can do the friend operator<<
Before the class definition, create a const integer that
defines the number of planets as 9. (Yes, I am counting Pluto for
the purposes of this assignment.)
The Planet class should have the following private
members:
*
* A std::string with the planet's name
* A double that is the mean distance from the sun
* A double that is the circulation time around the sun
* A double that is the rotation time around own its own
axis
* A long unsigned int that is the planet's equatorial radius
in kilometers ;
* A double that is the planet's mass compared to earth ;
* An unsigned int that is the planet's number of moons ;
You will need to create a constructor that takes in all these
values and stores them using a member initialization list.
* Remember that your member initialization list must be in the
same order as the private members are listed in the class
definition. Recall the reason why: we want to ensure effective
pointer arithmetic when allocating these elements, so putting them
in order will make your program run more efficiently.
Next, you should write a method that returns the Planet's
Name.
Next, you should write an overloaded friend operator that
prints out the information regarding the Planet. You may reference
the format below to see a good output format. Here are a couple of
crucial details to make it work:
* You will need to multiply the mean distance from the sun by
149500000 to get the number of kilometers
* Remember my "rules" for creating a friend operator<<
in .h and .cpp
* Remember the importance of const and call by reference in
the overloaded operator.
Function Library
For the second phase of the assignment, you will create
functions to call the Planet objects, and these functions will be
called in main
First, be sure to include the Planet.h library for the
class
Second, include string and vector libraries in this .h
file
You will need to write two functions in the .h/.cpp
files
void getPlanets( std::ifstream& ifs, std::vector<
Planet >& thePlanets );
In main, you will create an ifstream and a vector of Planets.
In this file, you will read in from the file information about the
Planets. The information is stored in the exact order that we made
in the class.
The data may be found here at PlanetData.txt:
Mercury 0.387 0.241 58.815 2433 0.05 0
Venus 0.723 0.615 224.588 6053 0.82 0
Earth 1.000 1.000 1.000 6379 1.00 1
Mars 1.523 1.881 1.029 3386 0.11 2
Jupiter 5.203 11.861 0.411 71370 317.93 12
Saturn 9.541 29.457 0.428 60369 95.07 10
Uranus 19.190 84.001 0.450 24045 14.52 5
Neptune 30.086 164.784 0.657 22716 17.18 2
Pluto 39.507 248.35 6.410 5700 0.18
Use the while( ifs.good() ) to loop through the file. In each
loop, create a temporary variable for each element, and then read
in using the ifs >> operator. Once read in, create a
temporary Planet using the constructor you wrote. Then, push that
temporary Planet onto the vector.
bool getPlanetNameFromUser( std::string& planetName
);
Prompt the user to enter a value from the command line, or
"End" to terminate. If the user entered end, call exit(-1) to end
the program. Otherwise, return true. planetName will store the
value. Calling planetName by reference will save the input.
Main Program
For the third phase of the question, you will develop the
program itself. In the main program:
* create a std::vector of Planets and an std::ifstream. (You
may hardcode the Planet file into this program instead of reading
from the command line)
* Then get the Planets from the file.
* Create a string to store the user's response
* Using a while loop, Get the Planet Name From the User and
use the bool to determine whether to continue
* When we go inside the loop
* Create a long unsigned int to loop
* Iterate through the vector, and compare the Planet's name in
each Planet in the vector
* For this internal while loop, it is important to check if
the long unsigned int is less than the number of planets, and then
check to see if the Planet's name is equal to the input
* Otherwise, you will get major issues with segmentation
faults
* After the while loop completes, make an if statement
* First, check if the iteration value is less than the number
of planets (for the same reason)
* Next, check if the name at the Planet at the iter location
is equal to the string input by the user
* If both tests pass, use the Planet's operator<< to
print the information to the user.