Question

In: Computer Science

C++ Question 2 You will read in data about the planets from a text file, and...

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.

Solutions

Expert Solution

Here is the answer for your question in C++ Programming Language.

Kindly upvote if you find the answer helpful.

################################################################

CODE :

Planet.h

#ifndef PLANET_H
#define PLANET_H
#include<string>
#include<vector>
#include<iostream>
#include<fstream>
//constant variable
const int planets = 9;
//Class planet
class Planet{
   //Public member functions
   public:
       Planet(std::string name,double mean,double circulation,double rotation,long unsigned int radius,double mass,unsigned int moons);
       std::string getPlanetName();
       friend std::ostream &operator<<(std::ostream &out,const Planet& planet);
   //Class' private member variables
   private:
       std::string planetName;
       double meanDistance;
       double circulationTime;
       double rotationTime;
       long unsigned int equatorialRadius;
       double planetMass;
       unsigned int moons;
};
#endif

###################################################################################

Planet.cpp

#include "Planet.h"
#include<iomanip>

//Constructor definition
Planet::Planet(std::string name,double mean,double circulation,double rotation,long unsigned int radius,double mass,unsigned int moons){
   this->planetName = name;
   this->meanDistance = (mean*149500000);
   this->circulationTime = circulation;
   this->rotationTime = rotation;
   this->equatorialRadius = radius;
   this->planetMass = mass;
   this->moons = moons;
}
//Method to get planet name
std::string Planet::getPlanetName(){
   return this->planetName;
}
//Overloaded cout operator
std::ostream &operator<<(std::ostream &out,const Planet& planet){  
   out << "======================================================================" << std::endl;
   out << std::left << std::setw(25) << "Planet " << std::fixed << std::setprecision(2) << planet.planetName << std::endl;
   out << std::left << std::setw(25) << "Mean distance from sun " << planet.meanDistance << " Kms." << std::endl;
   out << std::left << std::setw(25) << "Circulation Time " << planet.circulationTime << std::endl;
   out << std::left << std::setw(25) << "Rotation Time " << planet.rotationTime << std::endl;
   out << std::left << std::setw(25) << "Equatorial Radius " << planet.equatorialRadius << " Kms" << std::endl;
   out << std::left << std::setw(25) << "Planet Mass " << planet.planetMass << std::endl;
   out << std::left << std::setw(25) << "Number of moons " << planet.moons << std::endl;
   out << "======================================================================" << std::endl;
   return out;
}

#####################################################################################

mainPlanet.cpp

#include "Planet.cpp"

using namespace std;

void getPlanets( std::ifstream& ifs, std::vector<Planet>& thePlanets );
bool getPlanetNameFromUser( std::string& planetName );

vector<Planet> thePlanets;
int main(){
   ifstream inFile;
   inFile.open("PlanetData.txt");  
   string planetName;
   if(!inFile){
       cout << "File not found.." << endl;      
   }else{
       getPlanets(inFile,thePlanets);
       while(getPlanetNameFromUser(planetName)){
       }
   }
   return 0;
}
void getPlanets( std::ifstream& ifs, std::vector<Planet>& thePlanets ){
   std::string planetName;
   double meanDistance;
   double circulationTime;
   double rotationTime;
   long unsigned int equatorialRadius;
   double planetMass;
   unsigned int moons;
   while(ifs.good()){
       ifs >> planetName >> meanDistance >> circulationTime >> rotationTime >> equatorialRadius >> planetMass >> moons;
       Planet planet(planetName,meanDistance,circulationTime,rotationTime,equatorialRadius,planetMass,moons);
       thePlanets.push_back(planet);  
   }  
}
bool getPlanetNameFromUser( std::string& planetName ){
   cout << "Enter planet name : ";
   cin >> planetName;
   if(planetName.compare("END") == 0)
       exit(-1);
   int i = 0,flag = 0;
   while (i < planets){      
       if(thePlanets.at(i).getPlanetName().compare(planetName) == 0){
           flag = 1;
           cout << thePlanets.at(i);  
       }  
       i++;      
   }
   if(flag == 0)
       cout << "Given planet " << planetName << " not found." << endl;
   return true;
}

################################################################

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

###########################################################################

Planet.h

############################################################################

Planet.cpp

#############################################################################

main.cpp

############################################################################

PlanetData.txt

#############################################################################

OUTPUT :

Any doubts regarding this can be explained with pleasure :)


Related Solutions

In this lab, you will learn to read data from a sequential-access text file. To read...
In this lab, you will learn to read data from a sequential-access text file. To read data from a file, you need one FileStream object and one StreamReader object. The StreamReader object accepts the FileStream object as its argument. To read data from a sequential-access text file, here's what you need to do: From the desktop, open Visual Studio 2017. From the menu bar, navigate to File > New > Project. In the New Project window, select Windows Forms App...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt"...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt" (you will have to provide your own when debugging). The text file that is to be opened is formatted a certain way, namely, there is always one integer, one character (representing an operation), another integer, and then a new line character, with spaces between each item. A sample text file is provided below. theNumbers.txt 144 + 26 3 * 18 88 / 4 22...
C Programming How do you read in a text file line by line into a an...
C Programming How do you read in a text file line by line into a an array. example, i have the text file containing: line 1: "qwertyuiop" line 2: "asdfghjkl" line 3: "zxcvbnm" Then in the resulting array i get this: array:"qwertyuiopasdfghjklzxcvbnm"
You are required to read in a list of stocks from a text file “stocks.txt” and...
You are required to read in a list of stocks from a text file “stocks.txt” and write the sum and average of the stocks’ prices, the name of the stock that has the highest price, and the name of the stock that has the lowest price to an output file. The minimal number of stocks is 30 and maximal number of stocks in the input file is 50. You can download a input file “stocks.txt” from Canvas. When the program...
Problem Statement You are required to read in a list of stocks from a text file...
Problem Statement You are required to read in a list of stocks from a text file “stocks.txt” and write the sum and average of the stocks’ prices, the name of the stock that has the highest price, and the name of the stock that has the lowest price to an output file. The minimal number of stocks is 30 and maximal number of stocks in the input file is 50. You can download a input file “stocks.txt” from Canvas. When...
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays. Function prototypes: int readData(ifstream &iFile, int scores[][10], char names[][30]); This functions takes the file stream parameter inFile...
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
Download the file data.csv (comma separated text file) and read the data into R using the...
Download the file data.csv (comma separated text file) and read the data into R using the function read.csv(). Your data set consists of 100 measurements in Celsius of body temperatures from women and men. Use the function t.test() to answer the following questions. Do not assume that the variances are equal. Denote the mean body temperature of females and males by μFμF and μMμMrespectively. (a) Find the p-value for the test H0:μF=μMH0:μF=μM versus HA:μF≠μM.HA:μF≠μM. Answer (b) Are the body temperatures...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT