I'm currently working on a C program that can compute for thermal expansion of certain materials. Every time I enter the values, it keeps showing numbers like 1.01933e-038 and 3.09028e-41. Here's the code:
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
string material_name;
float temperature_change, length_of_material, displacement_in_meters, coefficient;
void theFormula(float temperature_change, float length_of_material, float coefficient) {
displacement_in_meters = temperature_change * length_of_material * coefficient;
}
int main()
{
char Continue;
string material_name;
float coefficient_steel = 1.20*pow(10,-5);
float coefficient_concrete = 14.5*pow(10,-6);
float coefficient_glass = 8.50*pow(10,-6);
float coefficient_aluminum = 1.20*pow(10,-5);
float coefficient_copper = 1.70*pow(10,-5);
float coefficient_pyrex = 4.0*pow(10,-6);
float temperature_change, length_of_material, displacement_in_meters, coefficient;
do {
cout << "What is the name of the material?" << "\n";
cin >> material_name;
transform(material_name.begin(), material_name.end(), material_name.begin(), ::toupper);
cout << "What is the length of the " << material_name << "?" << "\n";
cin >> length_of_material;
cout << "What is the temperature change (in degrees celsius)?" << "\n";
cin >> temperature_change;
if (material_name == "STEEL") {
theFormula(temperature_change, length_of_material, coefficient_steel);
cout << material_name << " will have a displacement of " << displacement_in_meters << "\n";
}
else if (material_name == "PYREX") {
theFormula(temperature_change, length_of_material, coefficient_pyrex);
cout << material_name << " will have a displacement of " << displacement_in_meters << "\n";
}
else if (material_name == "COPPER") {
theFormula(temperature_change, length_of_material, coefficient_copper);
cout << material_name << " will have a displacement of " << displacement_in_meters << "\n";
}
else if (material_name == "CONCRETE") {
theFormula(temperature_change, length_of_material, coefficient_concrete);
cout << material_name << " will have a displacement of " << displacement_in_meters << "\n";
}
else if (material_name == "ALUMINUM") {
theFormula(temperature_change, length_of_material, coefficient_aluminum);
cout << material_name << " will have a displacement of " << displacement_in_meters << "\n";
}
else if (material_name == "GLASS") {
theFormula(temperature_change, length_of_material, coefficient_glass);
cout << material_name << " will have a displacement of " << displacement_in_meters << "\n";
}
else {
cout << "The available options are: STEEL, PYREX, CONCRETE, GLASS, COPPER, and ALUMINUM.";
}
cout << "Type Y to continue, if not, type BYE." << "\n";
cin >> Continue;
}
while (Continue == 'Y' || Continue == 'y');
system("pause");
return 0;
}
I keep getting the same numbers no matter the values I put in. I checked the formula and the values for the coefficients, I can't seem to find what is wrong.
Edit:
Here is the edited version. I'm not just getting values of 0 instead of something like 0.0573048.
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
double theFormula(float temperature_change, float length_of_material, float coefficient) {
return temperature_change * length_of_material * coefficient;
}
int main()
{
char Continue;
string material_name;
float temperature_change, length_of_material, displacement_in_meters, coefficient;
displacement_in_meters = theFormula(temperature_change, length_of_material, coefficient);
do {
cout << "What is the name of the material?" << "\n";
cin >> material_name;
transform(material_name.begin(), material_name.end(), material_name.begin(), ::toupper);
cout << "What is the length of the " << material_name << "?" << "\n";
cin >> length_of_material;
cout << "What is the temperature change (in degrees celsius)?" << "\n";
cin >> temperature_change;
if (material_name == "STEEL") {
theFormula(temperature_change, length_of_material, 1.20E-5);
}
else if (material_name == "PYREX") {
theFormula(temperature_change, length_of_material, 4.0E-6);
}
else if (material_name == "COPPER") {
theFormula(temperature_change, length_of_material, 1.70E-5);
}
else if (material_name == "CONCRETE") {
theFormula(temperature_change, length_of_material, 14.5E-6);
}
else if (material_name == "ALUMINUM") {
theFormula(temperature_change, length_of_material, 2.31E-5);
}
else if (material_name == "GLASS") {
theFormula(temperature_change, length_of_material, 8.50E-6);
}
else {
cout << "The available options are: STEEL, PYREX, CONCRETE, GLASS, COPPER, and ALUMINUM.";
}
cout << material_name << " will have a displacement of " << displacement_in_meters << "\n";
cout << "Type Y to continue, if not, type BYE." << "\n";
cin >> Continue;
}
while (Continue == 'Y' || Continue == 'y');
system("pause");
return 0;
}
It's giving me a warning that the coefficient
is uninitialized.
CodePudding user response:
You are declaring multiple variables with identical names. You assign to one variable but then use the other. That's why they have a garbage values.
One way to avoid this problem is not to use any global variables. Here's a quick example
// no global variables here
// theFormula returns a float
float theFormula(float temperature_change, float length_of_material, float coefficient) {
// return the value, don't assign to a global variable
return temperature_change * length_of_material * coefficient;
}
int main()
{
...
// assign the returned value to a local variable
displacement_in_meters = theFormula(temperature_change, length_of_material, coefficient_steel);
// use the local variable
cout << material_name << " will have a displacement of " << displacement_in_meters << "\n";
That a function can return
a value seems to be something that newbies have trouble grasping, which then can lead to inappropriate use of global variables as in your case.