Trouble reading float variables.
I tried reading some float variables from a file. This is what happened.
Input.(input file).
Code:
Hydrogen H G 1 1.008
Helium He G 2 4.0
Lithium Li S 3 6.94
Beryllium Be S 4 9.01
Boron B S 5 10.8
Carbon C S 6 12.0
Nitrogen N G 7 14.0
Oxygen O G 8 16.0
Fluorine F G 9 19.0
Neon Ne G 10 20.2
Sodium Na S 11 23.0
Magnesium Mg S 12 24.30
Aluminium Al S 13 27.0
But output.(both screen and output file).
Code:
Hydrogen H 1 1.008 G
Helium He 2 4 G
Lithium Li 3 6.94 S
Beryllium Be 4 9.01 S
Boron B 5 10.8 S //works ok
Carbon C 6 12 S
Nitrogen N 7 14 G
Oxygen O 8 16 G
Fluorine F 9 19 G
Neon Ne 10 20.200001 G //decimals.
Sodium Na 11 23 S
Magnesium Mg 12 24.299999 S //decimals
Aluminium Al 13 27 S
My doubt...
Is there any way to avoid this.?
I even tried editing the file as:
Code:
Neon Ne G 10 20.20
and
Neon Ne G 10 20.20000000000
here's the part reading the file...
Code:
Element table[20]; class with details of an element...
int n=0;
char na[25], sy[3], st='\0'; //name symbol and state of element...
unsigned int z=0;
float m=0.0;
while(!file.eof()){
file>>na>>sy>>st>>z>>m;
if(file.good()){
table[n].set(na, sy, st, z, m); //tested - OK
n++;
}
}
cout<<"There are currently "<<n<<" entries in the input file...\n\n";