// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
usingnamespace std;
// Prototypes
double kineticEnergy(double mass , double meters);
double getKE();
// Main Function
int main()
{
double mass , meters , KE , Energy;
cout << "This will calculate how much kinetic energy an object has.\n\n";
cout.setf(ios_base::fixed , ios_base::floatfield);
cout.precision(2);
KE = getKE();
Energy = kineticEnergy(mass , meters);
cout << "The object with a mass weight and velocity of " << Energy << " kilograms and meters." << KE << endl;
_getch();
return 0;
}
// Get Kinetic Energy Function
double getKE()
{
double mass , meters;
cout << "Enter the objects weight in kilograms: ";
cin >> mass;
cout << "Enter the objects velocity in meters: ";
cin >> meters;
cout << "\n\n";
return mass;
return meters;
}
// Kinetic Energy Function
double kineticEnergy(double mass , double meters)
{
return (1.0 / 2.0) * (mass * meters) * 2;
}
Im having a problem at line 22, when i go to compile it get 2 errors saying
unitialized local varibales mass and meters used.
I need the kinetic energy function to return that result to where its called in the function, any ideas on how to fix this.
A function can only return one piece of data. When two "return" statements are contiguous, the second "return" is ignored completely. To overcome this restriction, you have a few options:
1) declare two "double" formal parameters of type "double &" and assign the values to them.
2) declare a structure [with two "double" members] and return an instance of it; using "mass" and "meters" to initialise the instance. For example:
Isn't that the wrong formula for K.E. ?
It should be ½mv²
where m is the mass, v is the velocity (in metres per second).
that can be expressed as 0.5 * m * v * v
Sorry, I know my remarks are off-topic in a C++ discussion, but it did look to me as though "squared" had been misinterpreted as "multiply by two".
(1.0 / 2.0) * (mass * meters) * 2;
and (1.0 / 2.0) * (mass * meters * meters);
will give identical results for the case where meters == 2
(or meters == 0)
It will still compile and execute either way, but of course the results will be different.
Edit:
In fact this expression (1.0 / 2.0) * (mass * meters) * 2 will simplify, the 1/2 and *2 cancel out. That just leaves mass * meters which is the formula for a different quantity, linear momentum.
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
usingnamespace std;
// Prototypes
double kineticEnergy(double mass , double meters);
double getKE(); //returns one value
// Main Function
int main()
{
double mass , meters , KE , Energy; //(*) these variables
cout << "This will calculate how much kinetic energy an object has.\n\n";
cout.setf(ios_base::fixed , ios_base::floatfield);
cout.precision(2);
KE = getKE();
Energy = kineticEnergy(mass , meters);
cout << "The object with a mass weight and velocity of " << Energy << " kilograms and meters." << KE << endl;
_getch();
return 0;
} // (*) die here
// Get Kinetic Energy Function
double getKE()
{
double mass , meters; //these are different that the ones on (*)
cout << "Enter the objects weight in kilograms: ";
cin >> mass;
cout << "Enter the objects velocity in meters: ";
cin >> meters;
cout << "\n\n";
return mass;
return meters; //this line is never executed
}
// Kinetic Energy Function
double kineticEnergy(double mass , double meters)
{
return (1.0 / 2.0) * (mass * meters) * 2; //this equation is incorrect
}
int main()
{
double mass , meters , KE , Energy;
cout << "This will calculate how much kinetic energy an object has.\n\n";
cout.setf(ios_base::fixed , ios_base::floatfield);
cout.precision(2);
getKE(mass, meters); //stores mass & meters values to vars in main.
Energy = kineticEnergy(mass, meters); //passes new values.
//+ cout statement here
_getch();
return 0;
}
// Get Kinetic Energy Function
double getKE(double & mass, double & meters) // pass by reference
{
double mass , meters;
cout << "Enter the objects weight in kilograms: ";
cin >> mass;
cout << "Enter the objects velocity in meters: ";
cin >> meters;
cout << "\n\n";
return mass;return meters;
}
// Kinetic Energy Function
double kineticEnergy(double mass , double meters)
{
return (1.0 / 2.0) * (mass * meters) * 2; //this equation is incorrect
}
It means you are passing by reference. When you don't have them, you are passing by value. When passing by value, new copies of the variables are made in the new function, and the values of the original variables don't change. When you pass by reference, you are sending a 'reference' to the variables themselves, so whatever operations you perform on those variables in said function will affect the values of the variables.