Reputation: 23
I have some experience in C++. I wrote the following code;
#include <iostream>
using namespace std;
double mass_H;
double mass_S;
double Mass;
double S_Space;
double H2S_Conc;
double Conc;
// calculates the molecular mass of H2S, given the molecular mass of each element
void Molecular_Mass()
{
Mass = ((2*mass_H)+(mass_S));
}
void H2S_Concentration()
{
Conc = H2S_Conc/100;
//c = H2S_Conc/1000000;
}
int main()
{
cout << "Welcome to the H2S conversion .\n";
cout << "Please enter molecular mass of Hydrogen: "<<endl; // enter mass of hydrogen
cin >> mass_H;
cout << "Please enter molecular mass of Sulphur: " << endl; // eneter mass of sulpher
cin >> mass_S;
Molecular_Mass();
H2S_Concentration(); // call molecular mass function
S_Space = 22.41; //Nm^3/kmol
H2S_Conc = 292; // ppm v/v
cout << "Molecular mass of H2S is: " << Mass << "kg/kmol"<< endl;
cout << "Standard space occupied by a kmol at 0 degrees and 1 atm is: "<< S_Space << "Nm^3/kmol" << endl;
cout << "H2S concentration is: " << H2S_Conc << "parts per million, which is a real concentration of: " << Conc <<endl;
The problem is when I compile and run, my value for Conc is 0. I am dividing two doubles, which I thought wasn't a problem.
I have had a look on google and I tried casting, by using a float but that didnt work either.
Can anyone tell me why it is returning zero
p.s ignore the rest of my crappy code ;)
Upvotes: 0
Views: 3838
Reputation: 101
the error has been answered but you could edit your code a bit. For example:
float H2S_Concentration()
{
return H2S_Conc/100;
//c = H2S_Conc/1000000;
}
then in the main
S_Space = 22.41; //Nm^3/kmol
H2S_Conc = 292; // ppm v/v
Molecular_Mass();
Conc = H2S_Concentration();
Upvotes: -2
Reputation: 9407
It is taking its default initialization value 0
since you did not assign any value to it before calling your function.
This should work:
#include <iostream>
using namespace std;
// ..
int main()
{
// ..
S_Space = 22.41; //Nm^3/kmol
H2S_Conc = 292; // ppm v/v
// ..
H2S_Concentration(); // call molecular mass function here
//..
return 0;
}
Edit: As recommended by πάντα ῥεῖ, and since you are hard coding their values and not changing or will not be changing these values at run time, it is recommended to set both variables as constants, and initialize them with the wanted values like this:
const double S_Space = 22.41; //Nm^3/kmol;
const double H2S_Conc = 292; // ppm v/v;
Upvotes: 2
Reputation: 1623
Call H2S_Concentration()
after these 2 lines :-
S_Space = 22.41; //Nm^3/kmol
H2S_Conc = 292; // ppm v/v
Upvotes: 3