C++ I\'m trying to get a text file that\'s petty simple to inport values into a
ID: 3557718 • Letter: C
Question
C++
I'm trying to get a text file that's petty simple to inport values into a Class. For some reason I can't get this thing to work right. I've tried a couple of different approaches. The best I get is it will assign values of 0 to everything, but that's not what I want...
Any thoughts?
Thanks,
Dave
////////////
//Text File Contents
330000
0.1
6800
0.05
70.9606
61.4378
82.9949
H
//end of text file
////////////////
void Filter::InFile()
{
// displays the infile, but doesn't assign to object...
string fname;
cout << "Enter the file name: " << endl;
cin >> fname;
ifstream fin;
fin.open(fname);
if (!fin)
{
cout << "Unable to retrive Target file name...";
return;
}
else
{
while (fin)
{
string line0, line1, line2, line3, line4, line5, line6,
line7 = "";
fin >> line0 >> line1 >> line2 >> line3 >> line4 >> line5 >> line6 >> line7;
cout << "All lines: " << line0 << line1 << line2 << line3 <<
line4 << line5 << line6 <<line7 << endl;
double resNom = atof(line0.c_str());
double resTol = atof(line1.c_str());
double capNom = atof(line2.c_str());
double capTol = atof(line3.c_str());
double coFreq = atof(line4.c_str());
double mincoFreq = atof(line5.c_str());
double maxcoFreq = atof(line6.c_str());
char type = line7[0];
FilterRes0.setNominal(resNom);
FilterRes0.setTolerance(resTol);
FilterCap0.setNominal(capNom);
FilterCap0.setTolerance(capTol);
setCutOffFreq(coFreq);
setMaxCutOffFreq(mincoFreq);
setMinCutOffFreq(maxcoFreq);
setType(type);
}
}
fin.close();
}
Explanation / Answer
//Text File Contents
330000
0.1
6800
0.05
70.9606
61.4378
82.9949
H
//end of text file
////////////////
void Filter::InFile()
{
// displays the infile, but doesn't assign to object...
string fname;
cout << "Enter the file name: " << endl;
cin >> fname;
ifstream fin;
fin.open(fname.c_str());v// this line has bug...
if (!fin)
{
cout << "Unable to retrive Target file name...";
return;
}
else
{
if (fin) // BUG IS HERE DONT USE While use if.
{
string line0, line1, line2, line3, line4, line5, line6,
line7 = "";
fin >> line0 >> line1 >> line2 >> line3 >> line4 >> line5 >> line6 >> line7;
cout << "All lines: " << line0 << line1 << line2 << line3 <<
line4 << line5 << line6 <<line7 << endl;
double resNom = atof(line0.c_str());
double resTol = atof(line1.c_str());
double capNom = atof(line2.c_str());
double capTol = atof(line3.c_str());
double coFreq = atof(line4.c_str());
double mincoFreq = atof(line5.c_str());
double maxcoFreq = atof(line6.c_str());
char type = line7[0];
FilterRes0.setNominal(resNom);
FilterRes0.setTolerance(resTol);
FilterCap0.setNominal(capNom);
FilterCap0.setTolerance(capTol);
setCutOffFreq(coFreq);
setMaxCutOffFreq(mincoFreq);
setMinCutOffFreq(maxcoFreq);
setType(type);
}
}
fin.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.