HELP End of File

I'm completely baffled as to why my code refuses to read the last line in the "Numbers.txt file.

Looks like this:

Copies Sold Selling Price
4360 35.29
10500 20.99
50000 7.50
38500 104.00
80000 200.00


#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
ifstream infile;
ofstream outfile;
infile.open("y:\\Numbers.txt");
outfile.open("y:\\Results.txt", ios::app);

int bestoption; //used to hold whichever option is the best option for the user
int copiesSold; //used to hold the copies sold value
double sellingPrice; //used to hold the selling price value
double option1; //defines option 1
double option2; //defines option 2
double option3; //defines option 3

if (!infile)
{
cout << ("Cannot open or locate input file.") << endl;
system ("pause");
return 0;
}
infile.ignore(100,'\n');
infile >> copiesSold >> sellingPrice;

while (!infile.eof())
{
option1 = 5000 * 20000;
option2 = copiesSold * sellingPrice / 0.125;

if (copiesSold <= 4000)
option3 = 0.10 * (copiesSold * sellingPrice);
else
option3 = 0.14 * (copiesSold * sellingPrice);

if (option1 > option2 && option3)
bestoption = 1;
else
if (option2 > option1 && option3)
bestoption = 2;
else
bestoption = 3;

outfile << left << ("For") << ' ' << copiesSold << ' ' << ("copies sold at a price of $") << sellingPrice << "," << ' ' << ("the author is paid:") << endl;
outfile << left << "$" << setw(20) << fixed << setprecision(2) << option1 << ("under Option1") << endl;
outfile << left << "$" << setw(20) << fixed << setprecision(2) << option2 << ("under Option2") << endl;
outfile << left << "$" << setw(20) << fixed << setprecision(2) << option3 << ("under Option3") << endl;
outfile << left << ("Option") << ' ' << bestoption << ' ' << ("is the best choice.") << endl;
outfile << endl << endl << endl;

infile >> copiesSold >> sellingPrice;
}

outfile.close();
return 0;

}
I don't find an infile.close(); in your code. I think this is the problem :)
change the while statement to:

 
while(infile >> copiesSold >> sellingPrice)


Then delete the other lines above and at the end of the loop that take infile input. Better to just test the stream itself than eof().
Thank you, both. I will try it and let you know how it comes out!
YES!!!!! That worked. Both of you were correct.

THANKS SO MUCH!
Topic archived. No new replies allowed.