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;
}