-3

I wrote a programm that asks the user for three values and then calculates other values out of them.

But somehow it returns trash. For example this:

Track.h

class Track : public World
{
    public:

        friend class Vehicle;

        Track();
        virtual ~Track();
        float GetLenght() {return m_Lenght;}
        float GetSlope() {return m_Slope;}
        void SetSlope(float value) {m_Slope = value;}
        float GetAcceleration();
    protected:
    private:
        const float m_Lenght = 2; //m
        float m_Slope;
        float m_TrackAcceleration;
};

Track.cpp

#include "Track.h"
#include "World.h"
#include <math.h>
#include "Vehicle.h"

#define PI 3.14159265359

[...]

float Track::GetAcceleration() {
    World *Earth = new World();
    Vehicle *Car = new Vehicle();
    m_TrackAcceleration = Earth->Accelerate(Car->GetWeight())*sin(1*PI/180);
    return m_TrackAcceleration;
}

main.cpp

int main()
{
    World *Earth = new World();
    Track *Track1 = new Track();
    Vehicle *Car = new Vehicle();
    Mass *Mass1 = new Mass();

    float MassWeight, VehicleWeight, Slope;

    cout << "Mass Weight:"; cin >> MassWeight;
    cout << "Vehicle Weight:"; cin >> VehicleWeight;
    cout << "Slope:"; cin >> Slope;

    Mass1->SetWeight(MassWeight);
    Car->SetWeight(VehicleWeight);
    Track1->SetSlope(Slope);

    cout << "Acceleration Force:" << Track1->GetAcceleration() << endl << endl;
};

Vehicle.h

class Vehicle : public World
{
    public:

        friend class Track;

        Vehicle();
        virtual ~Vehicle();
        float GetWeight() {return m_VehicleWeight;}
        void SetWeight(float value) {m_VehicleWeight = value;}
        float GetSpeed(float seconds);
        float GetAcceleration();
        float GetDistance(float seconds);
    protected:
    private:
        float m_VehicleWeight;
        float m_Speed;
        float m_Distance;
        float m_VehicleAcceleration;
};

World.h

class World
{
    public:
        World();
        virtual ~World();
        float GetGravity (){return m_Gravity;}
        float Accelerate (float mass);
    protected:
    private:
        const float m_Gravity = 9.81; // m/s^2
        float m_WorldAcceleration;
};

should return 0,171208...., but it returns 1.91825e-039 with Mass Weight=1,Vehicle Weight=1 and Slope=1.

any idea on this?

user3662357
  • 81
  • 1
  • 8
  • 1
    What is `m_Slope`? What is it declared as? In addition, hopefully you're not doing this in your actual code, as you have memory leaks and dangling pointers in that function. You need to post a real function with values we can all see, or you'll be downvoted to Hades. – PaulMcKenzie May 27 '14 at 15:32
  • What does `Earth->Accelerate(Car->GetWeight())` return? What is the value of `m_Slope`? – Dialecticus May 27 '14 at 15:37
  • You gave us expected output and actual output, but we also need the corresponding input. – Dialecticus May 27 '14 at 15:38
  • `Earth->Accelerate(Car->GetWeight())` returns 9.81, which is the correct value, because `9.81*1=9.81`. m_Slope has the value `1`. – user3662357 May 27 '14 at 15:59
  • 1) You could have tested by using a 2 line program to ensure your calculations actually work by calling the `sin` function with the values you expect. 2) Your excessive usage of `new` and thus creating memory leaks has the signs of a Java programmer trying to write C++. Are you a Java programmer? – PaulMcKenzie May 27 '14 at 16:32
  • 1) `cout << Earth->Accelerate(Car->GetWeight())*sin(Track1->GetSlope()*3.14/180);` returns the correct value, so the sin function should work. It still delivers trash without the sin function. Furthermore it seems like the `Car->GetWeight()` messes up the whole thing, because if I insert the value manually, everything is okay. 2) No, I'm only a beginner in C++, not a Java programmer. How do I prevent the memory leaks? `delete Earth;[...]`? – user3662357 May 27 '14 at 16:45
  • 1
    @user3662357 - 1) Look at my answer below. 2) You don't need to create objects with `new`. When I see objects created with `new` without a single call to `delete`, and especially if there is no reason whatsoever to create the objects using `new` it almost always is a Java or C# programmer trying their hand at C++, and making the mistake that C++ knows to deallocate the memory that was allocated. – PaulMcKenzie May 27 '14 at 16:55

2 Answers2

2

I believe the issue is that you are not aware of what happens when you create objects in a local scope. For some reason, your code creates two separate sets of Earth and Car objects.

float Track::GetAcceleration() 
{
    World *Earth = new World();   // this is a brand new World object
    Vehicle *Car = new Vehicle();  // this is a brand new Vehicle object

    // now you're calcuating acceleration with these brand new objects 
    m_TrackAcceleration = Earth->Accelerate(Car->GetWeight())*sin(1*PI/180);
    return m_TrackAcceleration;
}

Not only do you have a memory leak, you are creating new objects, and we have no idea what values newly constructed objects have since you didn't post the code for the constructor for World or Vehicle.

Then in main(), you do this:

int main()
{
   World *Earth = new World();  // a brand new object
   Track *Track1 = new Track();  // another brand new object
   Vehicle *Car = new Vehicle();  // yet another 
   Mass *Mass1 = new Mass();  // and another

   float MassWeight, VehicleWeight, Slope;
   cout << "Mass Weight:"; cin >> MassWeight;
   cout << "Vehicle Weight:"; cin >> VehicleWeight;
   cout << "Slope:"; cin >> Slope;

   Mass1->SetWeight(MassWeight);
   Car->SetWeight(VehicleWeight);
   Track1->SetSlope(Slope);

   // This call knows nothing about the Earth or Car objects you created in main().
   cout << "Acceleration Force:" << Track1->GetAcceleration() << endl << endl;
}

OK, you assign the values to the objects declared in main(), but when you call GetAcceleration, the function creates two brand new objects that have nothing to do with the ones created in main. Where is your reference to the objects you created in main()?

First, you need to realize that C++ isn't Java. You don't need new to create objects.

int main()
{
   World Earth;  // a brand new object
   Track Track1;  // another brand new object
   Vehicle Car;  // yet another 
   Mass Mass1;  // and another

   float MassWeight, VehicleWeight, Slope;
   cout << "Mass Weight:"; cin >> MassWeight;
   cout << "Vehicle Weight:"; cin >> VehicleWeight;
   cout << "Slope:"; cin >> Slope;

   Mass1.SetWeight(MassWeight);
   Car.SetWeight(VehicleWeight);
   Track1.SetSlope(Slope);

   // This call knows nothing about the Earth or Car objects you created in main().
   cout << "Acceleration Force:" << Track1.GetAcceleration() << endl << endl;
}

There are now no memory leaks in this code.

Now, in GetAcceleration, how is it going to know about the objects you created in main(), unless you pass them as parameters?

  float Track::GetAcceleration(World& theWorld, Vehicle& theVehicle) 
  {
    m_TrackAcceleration = theWorld.Accelerate(theVehicle.GetWeight())*sin(1.0*PI/180);
    return m_TrackAcceleration;
  }

The call from main() would then look like this:

   cout << "Acceleration Force:" << Track1.GetAcceleration(Earth, Car) << endl << endl;
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Now it tells me èrror: ´Vehicle´ has not been declared` in this line of the Track.h: `float GetAcceleration(World& theWorld, Vehicle& theVehicle);`. Vehicle.h is included – user3662357 May 28 '14 at 09:46
  • You need to fix your header files so that they use include guards. http://stackoverflow.com/questions/8020113/c-include-guards – PaulMcKenzie May 28 '14 at 14:31
0

Your Car vehicle is just created. Assuming it doesn't initialize the members to value different from zero you should get a value of zero after the multiplication.

1.91825e-039 is a value near to zero. The difference might be a result of the algebra you are doing with slope earth acceleration and so on.

At lest your question doesn't show why one should expect a different value.

harper
  • 13,345
  • 8
  • 56
  • 105