This is embarrasing. My name is Leonard Norwood Jr. And I have problem concerning my program that does a simple program of multiplying a person's weight times the weight of the planet equals the weight they would be if they were on a planet. And this has to be done on C++ using classes.
The complier I'm using is Bloodshed C++ 4.9.9.2.
First it started with a few linker errors that I dealt with by doing a project of my work on Console Application.
I simply took some stuff from my other files and added them to the project. after working them around it complied and linked ok, but the program only worked that far to that extent. Now I've got an infinite loop program on my first input if it is not a number, and another problem somewhere with while statement used in my implementation file Planet.cpp. And finally no results of a simple mathematical problem of just multiplication. Once I put in the weight and the planet's name it ends up 0.
I'm still coming up blank on my head with where I went wrong on my logic.
I need some help because I'm still getting stumped with this. I've been asked around by people who tell me a lot about various problems and some of it helped back then, right now despite that I'm still not cracking down on how my program isn't working completely, someone needs to help me kick this one with some details of where I did wrong and what to do to fix it. Give me an example, use part of my program if you need to come up with a solution if need be. I just need to finish this thing.
This is the header file for specification file Planet.h
Code blocks are created by indenting at least 4 spaces
... and can span multiple lines
class Planet
{
public:
// Constructors
Planet();
// Post: your weight and the Planet's weight set to Earth.
Planet(string);
// Takes in string value and sets it to planet name, or defaults to earth if not valid
float PlanetWt(float) const; //observer, uses earth weight as argument and returns weight on chosen planet
string properName() const; //observer, properly displays name of planet name
Code blocks are created by indenting at least 4 spaces
... and can span multiple lines
private:
string PlanetName;
};
Next up is my implementation file: Planet.cpp
Code blocks are created by indenting at least 4 spaces
... and can span multiple lines
using namespace std;
const float Mercury = 0.4155f;
const float Venus = 0.8975f;
const float Earth = 1.0f;
const float Moon = 0.166f;
const float Mars = 0.3507f;
const float Jupiter = 2.5374f;
const float Saturn = 1.0677f;
const float Uranus = 0.8947f;
const float Neptune = 1.1794f;
const float Pluto = 0.0899f;
Planet::Planet()
{
PlanetName = "Earth";
}
Planet::Planet(string SomePlanet)
{
char chPlanetLet;
string NewPlanet;
bool DataOK;
int i;
int strLength;
strLength = SomePlanet.length();
for (i = 0; i < strLength; i++)
{
chPlanetLet = toupper(SomePlanet[i]);
NewPlanet += chPlanetLet;
}
while (!DataOK)
{
if(NewPlanet == "MERCURY")
{
PlanetName = "Mercury";
DataOK = true;
}
else if (NewPlanet == "VENUS")
{
PlanetName = "Venus";
DataOK = true;
}
else if (NewPlanet == "MOON")
{
PlanetName = "Moon";
DataOK = true;
}
else if (NewPlanet == "MARS")
{
PlanetName = "Mars";
DataOK = true;
}
else if (NewPlanet == "JUPITER")
{
PlanetName = "Jupiter";
DataOK = true;
}
else if (NewPlanet == "SATURN")
{
PlanetName = "Saturn";
DataOK = true;
}
else if (NewPlanet == "URANUS")
{
PlanetName = "Uranus";
DataOK = true;
}
else if (NewPlanet == "NEPTUNE")
{
PlanetName = "Neptune";
DataOK = true;
}
else if (NewPlanet == "PLUTO")
{
PlanetName = "Pluto";
DataOK = true;
}
else
{
cout << "Invalid planet name, please enter the correct spelling for the planet..." << endl;
cin >> SomePlanet;
break;
}
}
}
float Planet::PlanetWt(float NewUserWt) const
{
float NewPLWT;
if (PlanetName == "MERCURY")
NewPLWT = NewUserWt * Mercury;
else if (PlanetName == "VENUS")
NewPLWT = NewUserWt * Venus;
else if (PlanetName == "MOON")
NewPLWT = NewUserWt * Moon;
else if (PlanetName == "MARS")
NewPLWT = NewUserWt * Mars;
else if (PlanetName == "JUPITER")
NewPLWT = NewUserWt * Jupiter;
else if (PlanetName == "SATURN")
NewPLWT = NewUserWt * Saturn;
else if (PlanetName == "URANUS")
NewPLWT = NewUserWt * Uranus;
else if (PlanetName == "NEPTUNE")
NewPLWT = NewUserWt * Neptune;
else if (PlanetName == "PLUTO")
NewPLWT = NewUserWt * Pluto;
return NewPLWT;
}
string Planet::properName()const
{
return PlanetName;
}
And finally my client code Planet2.cpp
using namespace std;
int main(int argc, char *argv[])
{
float UserWeight;
float NewUserWt;
string UserPN;
string NewUserPN;
cout << "Please enter your earth weight in pounds (e.g. 155)..." << endl;
cin >> UserWeight;
cout << "\nEnter a planet name to see what your weight would be there..." << endl;
cin >> UserPN;
Planet SomePlanet(UserPN); // calculate new object with user specified planet name
NewUserWt = SomePlanet.PlanetWt(UserWeight);
// calculates users weight in chosen planet
NewUserPN = SomePlanet.properName();
// stores properly typed planet name
cout << endl << endl << "Your weight on " << UserPN << " would be " << NewUserWt << endl;
system("PAUSE");
return EXIT_SUCCESS;
}