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

include <iostream>

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 = 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

include <cstdlib>

include "Planet.h"

include <conio.h>

include <iostream>

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;

}

I am having trouble reading your code, but I understand the difficulty in the new system. To start a code block simply skip a line and indent by for. As such
this does not work, even though as I type it it is highlighted like code, it will not show up as code because I didn't leave an empty line.

Leaving a space like the one above then four spaces gets us:

test test :)

Sorry about the above code. I am try to get across where I went wrong. i'm still not seeing what's wrong with my logic I know its there, Something's wrong with either of my client code and my implmentation code. XD. Uh I still couldn't get it right with the identation.

// Header file Planet.h

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;

};

include <iostream>

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 = 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 SomeUserWt) const
{
float NewPLWT;

 if (PlanetName == "MERCURY")
    NewPLWT = SomeUserWt * Mercury;
 else if (PlanetName == "VENUS")
      NewPLWT = SomeUserWt * Venus;
 else if (PlanetName == "MOON")
      NewPLWT = SomeUserWt * Moon;
 else if (PlanetName == "MARS")
      NewPLWT = SomeUserWt * Mars;
 else if (PlanetName == "JUPITER")
      NewPLWT = SomeUserWt * Jupiter;
 else if (PlanetName == "SATURN")
      NewPLWT = SomeUserWt * Saturn;
 else if (PlanetName == "URANUS")
      NewPLWT = SomeUserWt * Uranus;
 else if (PlanetName == "NEPTUNE")
      NewPLWT = SomeUserWt * Neptune;
else if (PlanetName == "PLUTO")
      NewPLWT = SomeUserWt * Pluto;

return NewPLWT;

}

string Planet::properName()const
{
return PlanetName;
}

include <cstdlib>

include "Planet.h"

include <conio.h>

include <iostream>

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;

}

Edited 4 Years Ago by nekoleon64

I am having trouble reading your code

Because he didn't bother using CODE Tags... Maybe he can repost the code properly?

You need to read Thomasz Muldner's C for Java Programmer's and get a sense of top down and bottom up modular programming plus start along the lines of the idioms it suggests that keep your problem solving skills improving, or something, instead of the spit at the space station thinking your going to build a 1, 123, 12345 triangle before you get to midnight because it's due in a week.

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