我有一些C ++经验。我写了以下代码;
#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;
问题是当我编译并运行时,我的Conc值为0.我正在划分两个双打,我认为这不是问题。
我看过谷歌,我尝试使用浮动但是也没有用。
任何人都可以告诉我为什么它会返回零
p.s忽略我剩下的糟糕代码;)
答案 0 :(得分:3)
在这两行之后致电H2S_Concentration()
: -
S_Space = 22.41; //Nm^3/kmol
H2S_Conc = 292; // ppm v/v
答案 1 :(得分:2)
它正在采用其默认初始化值[l.split() for l in finallist]
,因为在调用函数之前没有为其赋值。
这应该有效:
0
编辑: 正如πάντα ῥεῖ所建议的,并且由于您在运行时对其值进行硬编码而不更改或不会更改这些值,因此建议将这两个变量设置为常量,并使用所需的值对其进行初始化,如下所示: :
#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;
}
答案 2 :(得分:-2)
错误已得到解答,但您可以稍微编辑一下代码。 例如:
float H2S_Concentration()
{
return H2S_Conc/100;
//c = H2S_Conc/1000000;
}
然后在主
S_Space = 22.41; //Nm^3/kmol
H2S_Conc = 292; // ppm v/v
Molecular_Mass();
Conc = H2S_Concentration();