Solved error from ifstream in c++ noob I am

November 19, 2011 at 19:51:10
Specs: Windows XP
why am i getting numbers like 6.15862e-039 when i try to read say 0.15 from a file..here is my lines of code so far with the error everything else is good:::
well first the file it looks kinda like
label1 label2 label 3
0.5 0.10 0.15

so u get an idea..i teaching myself so sorry if i am illustrating this badly...so i have

ifstream reader;
ofstream writer; //for simplicity sake

i already declared the function so this is the definition now

float labresults (ifstream& reader,ostream& writer){
//variables
sting l1,l2,l3
float ,pot1,pot2,pot3 //for the decimals

reader>>l1>>l2>>l3; writer <<l1<<l2<<l3<<endl; //this part works great all the headings work
reader>>pot1>>pot2>>pot3; writer<<pot1<<setw(14)<<pot2<<setw(14)<<pot3<<endl;}

Now when i go i see things like 6.15862e-039 where pot 1 value suppose to be 0.5 and similar results for pot2 and pot 3...where am i going wrong?


See More: error from ifstream in c++ noob I am

Report •

✔ Best Answer
November 20, 2011 at 08:06:54
Few things from the source provided:
eds>>b1>>b2>>b3>>l4>>l5;
l4 and l5 are not defined.

eds>>results>>bv1>>bv2>>bv3
You're asking for 4 values, but your input file only has 3?

float processEmp(ifstream& eds, ofstream& pds)
You declare this as a float, but you never return any value.

VS wouldn't let me compile until I fixed those errors. Once I did, it ran without issue. Your input file isn't Unicode or anything like that, is it?

How To Ask Questions The Smart Way



#1
November 20, 2011 at 04:42:45
and i labeled the prototype wrong supposed to be ofstream and yea i still gettting prob



Report •

#2
November 20, 2011 at 05:54:05
where am i going wrong?
Not giving us compilable source code that'll demonstrate the behavior you're talking about, for one.

How To Ask Questions The Smart Way


Report •

#3
November 20, 2011 at 06:13:58

#include<iostream>
#include<string>
#include<cstdlib> // for definition of EXIT_FAILURE
#include<fstream> // this directive required for file streams
using namespace std;

//associate streams with external file names
#define infile "C:/selftaught/datfile.txt"
#define outfile "C:/selftaught/outfile.txt"

float processEmp(ifstream&, ofstream&);

int main()
{

ifstream eds; //input
ofstream pds;// output
float totalPayroll;

//prepare files for processing
eds.open(infile);
if (eds.fail())
{ cerr<<"ERROR : CANNOT OPEN"<<infile<<" for input." <<endl;
return EXIT_FAILURE;
}

pds.open(outfile);
if (pds.fail())
{ cerr<<"ERROR : CANNOT OPEN"<<outfile<<" for output." <<endl;
eds.close();
return EXIT_FAILURE;
}

processEmp(eds,pds);


// close files
eds.close();
pds.close();
system("pause");
return 0;

}

float processEmp(ifstream& eds, ofstream& pds)

{ string b1,b2,b3,results; float bv1,bv2,bv3;//b-building..bv-building value

eds>>b1>>b2>>b3>>l4>>l5;
pds<<b1<<b2<<b3<<endl; //for buildings..so far works perfectly writing to a file
eds>>results>>bv1>>bv2>>bv3;// to read the second line with the results and the value
pds<<results<<bv1<<bv2<<bv3<<endl; //to write the value to the out file
}//end of func

//datfile has: building1 building2 building 3

// results 0.1 0.2 0.3

//all in a txt file

//The roblem is I keep getting non float values for my results instead of 0.1 in bv1 i am getting some long crazy number;


Report •

Related Solutions

#4
November 20, 2011 at 06:20:06
i'm getting the first line successfully that is the building 1 building 2 building 3 heading but when its time for results and then values i getting 38.0361e+0337.93631e+0333.30469e-039...i know i have to put spaces but its the same values what do i have to do? i followed as much tutorials and codes but i seem to still be missing something

Report •

#5
November 20, 2011 at 08:06:54
✔ Best Answer
Few things from the source provided:
eds>>b1>>b2>>b3>>l4>>l5;
l4 and l5 are not defined.

eds>>results>>bv1>>bv2>>bv3
You're asking for 4 values, but your input file only has 3?

float processEmp(ifstream& eds, ofstream& pds)
You declare this as a float, but you never return any value.

VS wouldn't let me compile until I fixed those errors. Once I did, it ran without issue. Your input file isn't Unicode or anything like that, is it?

How To Ask Questions The Smart Way


Report •

#6
November 20, 2011 at 09:46:23
yea it was the values problem! everything is fine now thanks a million!

Report •

Ask Question