-
Reading data from files
I am having trouble getting the code below to work. My first problem is that the first checkSpecials function returns all but the first line.
The second checkSpecials function returns 0 for all values. I dont know if I am using the correct way to assign "names" to the vaules.
the input file looks something like this:
5 2 2 670.60
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
void checkSpecials(ifstream& infile)
{
cout << endl << "Display orders stored in file orders.dat: " << endl;
while (infile)
{
string temp;
getline(infile, temp);
cout << temp << endl;
}
}
void checkSpecials(ifstream& infile, ofstream& outfile)
{
int numSpecials = 0;
float commision = 0;
int noServed =0;
float avgSpent = 0;
int count = 0;
int value, members, specials, wine, bill, total;
infile >> members >> specials >> wine >> bill;
while(infile >> value)
{
noServed ++;
total += total;
avgSpent = ((total / noServed) + (total % noServed));
if((members >= 5) && (specials >= 4) && (wine >= 2))
{
numSpecials ++;
}
}
outfile << "Number of families that ordered the special: " << numSpecials << endl
<< "Commision earned from the special meal: " << commision << endl
<< "Average spent per person for the evening: " << avgSpent << endl;
}
int main()
{
ifstream infile;
ofstream outfile;
string outName = "c:\\datafiles\\result.dat";
string inName = "c:\\datafiles\\orders.dat";
int members, special, wine;
float total;
infile.open(inName.c_str());
outfile.open(outName.c_str());
outfile.precision(2);
if (!infile)
{
cout << "Cannot open infile " << infile << " Aborting!" << endl;
exit(1);
}
if (!outfile)
{
cout << "Cannot open oufile " << outfile << " Aborting!." << endl;
exit(1);
}
checkSpecials(infile);
checkSpecials(infile, outfile);
infile.close();
outfile.close();
return 0;
}
-
Well to read the file for the second time, you need to
- reset the eof() error state
- rewind to the beginning of the file.
-
Code:
void checkSpecials(ifstream& infile)
{
cout << endl << "Display orders stored in file orders.dat: " << endl;
while (infile)
{
string temp;
getline(infile, temp);
cout << temp << endl;
}
}
This is flawed and suffers from the same issue as using end-of-file testing to control loops. Test the read operation itself:
Code:
void checkSpecials(ifstream& infile)
{
string temp;
cout << endl << "Display orders stored in file orders.dat: " << endl;
while (getline(infile, temp))
{
cout << temp << endl;
}
}
-
Brilliant thank you. That solves my first problem.
Would you also be able to help with the second problem? With the input file containing a few lines in the format: 5 2 5 680.00. Am I correct in using Code:
infile >> members >> specials >> wine >> bill;
to input the values?
Code:
void checkSpecials(ifstream& infile, ofstream& outfile)
{
int numSpecials = 0;
float commision = 0;
int noServed =0;
float avgSpent = 0;
int count = 0;
int value, members, specials, wine, bill, total;
infile >> members >> specials >> wine >> bill;
while(infile >> value)
{
noServed ++;
total += total;
avgSpent = ((total / noServed) + (total % noServed));
if((members >= 5) && (specials >= 4) && (wine >= 2))
{
numSpecials ++;
}
}
outfile << "Number of families that ordered the special: " << numSpecials << endl
<< "Commision earned from the special meal: " << commision << endl
<< "Average spent per person for the evening: " << avgSpent << endl;
}