-1

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 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.

  • 2
    you declared the variable 3 times as far as I can see (global, as argument to the function and as a local variable in the function. My guess would be, that you assign to one of them and then try to use another. – BitTickler Sep 08 '22 at 06:25
  • 1
    Globals -- don't. Ever. – DevSolar Sep 08 '22 at 06:26
  • Did you read the warnings that your compiler gave you? There's at least two different kinds that will help you find your mistake. – Joseph Sible-Reinstate Monica Sep 08 '22 at 06:27
  • What do you mean by warnings? I'm still relatively new to C++ so it's a bit difficult to understand – Myamsar Catsume Sep 08 '22 at 06:29
  • 1
    Regarding warnings: see [Why should I always enable compiler warnings?](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) (also covers the "how") – JaMiT Sep 08 '22 at 06:33
  • 3
    Side note: instead of `14.5*pow(10,-6)` write `14.5E-6`. – j6t Sep 08 '22 at 06:36
  • 1
    For your code, you should aim for a [mre]. Drop the user input; instead assign values to your variables that demonstrate the issue. Pick one line of output that demonstrates the issue, and drop the others. Keep it simple, keep it focused. – JaMiT Sep 08 '22 at 06:36
  • Make the function `theFormula` return a `float`, the displacement, instead of `void` and use that value. The `displacement_in_meters` uninitialized variable that is declared and printed in `main` is not the one set in that function. – Bob__ Sep 08 '22 at 06:37
  • 2
    Compiler error means the code could not be interpreted into a runnable program. Compiler warning means the code could be translated into a runnable program but that program probably doesn't do what you want. Warnings are the first line of defense against runtime logic errors. When you don't have them turned on and turned up loud, you're probably going to do more debugging than you needed to do. – user4581301 Sep 08 '22 at 06:38
  • `displacement_in_meters` inside `main` is uninitialised, you set the global version instead which is hidden by your local version in `main` – Alan Birtles Sep 08 '22 at 06:41
  • How do I initialize the displacement_in_meters? – Myamsar Catsume Sep 08 '22 at 06:46
  • Remove your global variables and return the value from the function – Alan Birtles Sep 08 '22 at 07:00
  • Sorry, but your question is more confusing after the edit than it was before. Which code am I supposed to look at? Why is that code so long? I'll again suggest a (single) [mre]. – JaMiT Sep 08 '22 at 13:09

1 Answers1

0

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.

john
  • 85,011
  • 4
  • 57
  • 81
  • The value it always give me is 0, rather than something like 0.0573048. I tried changing it to a double but still the same. – Myamsar Catsume Sep 08 '22 at 06:59
  • @MyamsarCatsume I'd have to see the latest code to know what is wrong. You could **add** (don't replace) the latest version of your code to the question and I'll take a look. – john Sep 08 '22 at 07:01
  • I've added the latest version. – Myamsar Catsume Sep 08 '22 at 07:08
  • @MyamsarCatsume So you missed out the assignment to the variable `displacement_in_meters = theFormula(temperature_change, length_of_material, 1.20E-5);` – john Sep 08 '22 at 07:15
  • Oh my, I did not see that, thanks for pointing it out! – Myamsar Catsume Sep 08 '22 at 07:18
  • @MyamsarCatsume I also notice that you are calling the function twice. Once before the `do` loop and once inside the `do` loop You don't need the one before the do statement. (which is also where your warning is coming from). – john Sep 08 '22 at 07:19