Let me try again my name is Leonard Norwood Jr.

I'm doing my homework where I have to write a program that outputs the users's weight on different planets.
Should sound familiar

With classes, I need to have one default constuctor that will create an object representing earth. The class as an observer operator that takes a weight on EArth as argument and returns the weight of the planet, Another observer takes the rest of the planets, returns them as string with proper capitalization.

// Header file Planet.h (Specification file)

include <string>

using namespace std;
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

  private:
  string PlanetName;

};

//main project file PnWclient.cpp

include "Planet.h"

include <string>

include <iostream>

using namespace std;

int main()
{
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;


return 0;

}

//implementation file Planet.cpp

include <iostream>

include <string>

include "Planet.h"

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 = 1;
         }
         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 = "Neptune";
              DataOK = true;
         }
         else
         {
             cout << "Invalid planet name, please enter the correct spelling for the planet..." << endl;
             cin >> SomePlanet;
         }
   }

}

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;
}

This is the error I got from Planet.cpp
[Linker error] undefined reference to `WinMain@16'
ld returned 1 exit status

And these errors are from the PnWclient.cpp
C:\Users\Leonard\AppData\Local\Temp/ccKadaaa.o(.text+0x234):PnWclient.cpp: undefined reference to Planet::Planet(std::string)' C:\Users\Leonard\AppData\Local\Temp/ccKadaaa.o(.text+0x299):PnWclient.cpp: undefined reference toPlanet::PlanetWt(float) const'
C:\Users\Leonard\AppData\Local\Temp/ccKadaaa.o(.text+0x2ae):PnWclient.cpp: undefined reference to `Planet::properName() const'
collect2: ld returned 1 exit status

So what do I do about these linker errors?

I'm not entirely sure, but if you're working in Visual Studio, it gets very fussy about the definition of main(). I'd recommend creating a new project of the desired type ("Console Application" is probably best), and then including "Planet.h" and any other additional includes at the top, and pasting the body of your existing main() into whatever version of main() it provides for you. Make sure you re-add your Planet.cpp file to the project as well.

I think that you have a few problems with this, but please put all of the code into a single code block and we can all compile and check it -- given how difficult I am finding the new coding highlighting system, I can full understand you making the mistake you made formating your code.

I have noticed the following:

Your constructor Planet(string); has an infinite loop in it as some_planet is not converted into NewPlant except in the first pass.

However, somewhere along the way, you have missed sight of the object orientated part:

You don't have a planet class set up like this:

class Planet
  {
   private:
    const std::string Name;
    const float conv_mass;
   public:
     // other stuff
    float weight(const float EarthWeight) const { return conv_mass*EarthWeight; } 

  };

Note what has happened there: object knows about its own mass, you don't need to do a whole lot of looking up for each question each time, you would use it like this:

int main()
  {
     Planet UR("Uranus");

     std::cout<<"My weight on Uranus == "<<UR.weight(72.0)<<std::endl;
     std::cout<<"My dogs weight      == "<<UR.weight(8.0)<<std::endl;
  }

The trick/pattern here is to define/set conv_mass in the constructor.

Edited 4 Years Ago by StuXYZ

This article has been dead for over six months. Start a new discussion instead.