Help with a calculator program.

I am making a little physics calculator for fun and I have run into a problem, I have a physics class with member functions to set and get values as well as to check if it possible to solve for an asked for value and if so do it. everything seems to be working fine but my problem is that at some point the proper answer gets replaced with -0.61728. I do initialize everything to -1.23456 so I can check if a value has been input yet but I don't think that should change anything. any help would be greatly appreciated. Oh and it does work correctly with force, mass and acceleration.

main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "Physics.hpp"
#include <cmath>
using namespace std;

int main()
{
    Physics calculator;
    calculator.setValue("vel2", 3);
    calculator.setValue("vel1", 5);
    //calculator.setValue("force", 11);
      calculator.setValue("mass", 2);
      calculator.setValue("time", 10);
    calculator.setValue("distance", 5);
   if (calculator.calculateValue("acceleration") == -1)
   {
       return -1;
   }
    cout << "v1: " << calculator.getValue("vel1") << " v2: " << calculator.getValue("vel2") << " distance: " << calculator.getValue("distance") << endl;
    cout << "acceleration is: " << calculator.getValue("acceleration") << calculator.acceleration << endl;
    return 0;
}


Physics class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Physics
{
protected: 
    float acceleration, vel1, vel2, mass, vel_ave, time, distance, force;
public:
    /* initialises all variables to -1.23456 a value which
     * I hope will never be an answer
     */
    Physics();
    // self explanitory
    void setValue(string name, float value);
    // returns the value the user asks for
    float getValue(string name);
    /* ensures that all the needed values are there and finds 
     * the asked for value then returns 1 on succes otherwise -1
    */
    int calculateValue(string name);
};


the problematic function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int Physics::calculateValue(string name)
{
    if (name == "acceleration")
    {
        if (force != -1.23456 && mass != -1.23456)
        {
            acceleration = force / mass;
            return 1;
        }
        else if (vel1 != -1.23456 && vel2 != -1.23456 && distance != -1.23456)
        {
           float a = pow(vel2, 2);
           float b = pow(vel1, 2);
           float c = a - b;
           float d = 2 * distance;
           acceleration = c / d;
           return 1;
        }
        else if (vel1 != -1.23456 && distance != -1.23456 && time != -1.23456)
        {
            float a, b, c;
            a = 2 * distance;
            b = 2 * vel1;
            a = a - b;
            acceleration = a / time;
            return 1;
        }
        else 
        {
            return - 1;
        }
    }
}


Sorry for all the text and thanks in advance.
Topic archived. No new replies allowed.