0

I am writing code to call functions having to do with force, mass, and acceleration equations. The functions are called correctly, but the inputs are not multiplied as they should be. My output for the first function is a crazy small number, and the output for the first function is always 0. Here is my code. Any feedback wold be very helpful. Thanks.

#include <iostream>
#include <cstdlib>

using namespace std;

void displayMenu();
double force(double);
double secondForce(double,double);

int main(int argc, char** argv) 
{
    int menuOption;
    displayMenu();
    system("PAUSE");
    return 0;
}

void displayMenu(void)
{
    int menuOption;
    double weight, accel;
    cout << "           Main Menu" << endl;
    cout << "Enter 1 for Force calculation with acceleration = 9.8m/s^2.\n";
    cout << "Enter 2 for Force calculation with user defined acceleration.\n";
    cout << "Enter 3 to quit the program.\n";
    cin >> menuOption;
        if(menuOption==1)
        {
            cout << "Enter a mass.\n";
            cin >> weight;
            cout << "The force is ";
            cout << force(weight);  
            cout << "N.";
            }
        else if(menuOption==2){
            cout << "Enter a mass.\n";
            cin >> weight;
            cout << "Enter an acceleration.\n";
            cin >> accel;
            cout << "The force is ";
            cout << secondForce(weight, accel);
            cout << "N.";
        }
}

double force(double weight)
{
    double force, mass;
    force=(mass*(9.8));
    return force;
}

double secondForce(double secondMass, double secondWeight)
{
    double secondForce, mass, acceleration;
    secondForce=(mass*acceleration);
    return secondForce;

}
ben delany
  • 13
  • 2
  • 1
    You use `mass` and `acceleration` without initializing them with any value. You are probably multiplying with random garbage. – melak47 Oct 08 '15 at 16:29

4 Answers4

2

You are using uninitialized variables in your functions. Using these uninitialized variables in your program is undefined behavior.

force=(mass*(9.8)); << mass has a garbage value
secondForce=(mass*acceleration); << mass and acceleration have a garbage value

I think you meant to have

double force(double weight)
{
    return weight * 9.8;
}

double secondForce(double secondMass, double secondAccel)
{
   return secondMAss * secondAccel;
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • I think the second sentence is not 100% correct, because an uninitialised `unsigned char` does not cause undefined behaviour but only an unspecified value. (Not that it matters much here.) – Christian Hackl Oct 08 '15 at 16:50
  • @ChristianHackl where does it call that out? – NathanOliver Oct 08 '15 at 17:01
  • I meant that "using an uninitialized variable is undefined behavior" is true for all types but `unsigned char`. – Christian Hackl Oct 08 '15 at 17:02
  • @ChristianHackl Yes and I would like to know where that is called out. I have not seen that before. – NathanOliver Oct 08 '15 at 17:07
  • Sorry, I misunderstood your comment. Some sources (search inside of them for "unsigned char"): http://www.ibm.com/developerworks/library/pa-ctypes3/, http://stackoverflow.com/questions/6725809/trap-representation/6725981. I believe §3.9.1/1 is the official guarantee for C++: *"For unsigned narrow character types, each possible bit pattern of the value representation represents a distinct number. These requirements do not hold for other types."* No trap representation -> no UB when reading. – Christian Hackl Oct 08 '15 at 17:14
  • See also http://stackoverflow.com/questions/26249221/unsigned-narrow-character-type-number-representation – Christian Hackl Oct 08 '15 at 17:17
  • @ChristianHackl let me know if the change sounds better to you. – NathanOliver Oct 08 '15 at 17:34
  • Yes, I think it's now 100% correct. Thanks for incorporting this nitpick. – Christian Hackl Oct 08 '15 at 17:56
  • Thank you very much. Code is running perfectly now. – ben delany Oct 08 '15 at 18:25
1

The problem is you are using variables in your calculation that are undefined. See my embedded comments below:

double secondForce(double secondMass, double secondWeight)
{
    double secondForce, mass, acceleration;
    secondForce=(mass*acceleration); //what is the value of mass and acceleration here??
    return secondForce;
}

As a side note: Consider stepping through your code inside of a debugger like gdb so you can see how your program executes. Trying to reason through it is very difficult for large programs. In your example, it's easy to spot for an experienced programmer.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
  • While I certainly agree that learning to use a debugger is an excellent idea, I have to wonder how it would possibly help with this specific problem. The OP would see that the variables apparently contain strange random values and keep wondering why. – Christian Hackl Oct 08 '15 at 16:58
  • They would at least see that the variables have strange data and narrow the problem down to a single line of code. What makes you think the OP would not be able to correlate the strange data values to un-initialized variables? – Amir Afghani Oct 08 '15 at 17:18
  • What makes me think that? Well, experience as a teaching assistant in software-engineering classes at university... and my own past, back when I started learning C. – Christian Hackl Oct 08 '15 at 17:20
  • OK but you agree that they can narrow it down to a single line of code at least? – Amir Afghani Oct 08 '15 at 17:24
  • I tried defining these variables as 0.0 as some other comments suggested, but the answers still remain the same? How can I get the user to define what these variables are and then correctly use those values in my function? – ben delany Oct 08 '15 at 18:02
0

You're multiplying the data by garbage values

double force(double weight)
{
double force, mass;
force=(mass*(9.8));
return force;
}

double secondForce(double secondMass, double secondWeight)
{
double secondForce, mass, acceleration;
secondForce=(mass*acceleration);
return secondForce;
}

What's the value of mass and acceleration here? These variables are not initialized. You have to assign some values to them first.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • Re your last sentence: A better thing would be to initialise them. Leaving them uninitialised first and then assign something to them is an old C-ism completely unnecessary in C++. – Christian Hackl Oct 08 '15 at 16:59
0

You should enable compiler warnings and pay attention to them. For example, here is what MSVC says even on the lowest warning level:

warning C4700: uninitialized local variable 'mass' used
warning C4700: uninitialized local variable 'mass' used
warning C4700: uninitialized local variable 'acceleration' used

In C++, using an uninitialised variable is never a good idea. Unlike in other languages, most variables in C++ are not automatically initialised with 0 values. This causes your program to inhibit unspecified or undefined behaviour when the value contained in the variable is to be accessed.

Be safe and correct. Initialise! For example:

double force = 0.0;
double mass = 0.0;

Note that the language also allows you to first initialise, then assign, then use the variable. For example:

double force;
force = 0.0;
cout << force;

But that's bad style. Why leave a variable uninitialised and then later assign something when you can do everything in one step? Leaving the variable uninitialised first also prevents you from making it const.

Ensuring correct initialisation of all variables will make the behaviour of your program defined and consistent. You will have other problems, though, because you multiply the 0 mass, which will always result in 0 and is certainly not what you intend. However, deterministic behaviour is a good starting point to fix the wrong math.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
  • I tried defining these variables as 0.0 in the function, but the answers still remain the same? How can I get the user to define what these variables are and then correctly use those values in my function? – ben delany Oct 08 '15 at 18:03