Hey all, I have been working on this practice program for my coms class and was having some trouble figuring it out and was wondering if anyone could take a look at it for me? not sure how to paste a picture here so I'm just going to copy/paste the skeleton program, thanks - Mig
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
string name;
int numOfVolunteers;
int numOfBoxesSold;
int totalNumOfBoxesSold;
double costOfoneBox;
totalNumOfBoxesSold=0;
numOfVolunteers=0;
ifstream inFile;
ofstream outFile;
outFile.open("dataFile.txt");
inFile.open("inFile.txt");
if(!inFile)
{
cout << "File does not exist mi amigo" << endl;
}
else
{
outFile << fixed << showpoint << setprecision(2);
inFile >> costOfoneBox;
outFile << "Cost of one box: " << costOfoneBox << endl;
outFile << endl << left << setw(12) << "Name" << setw(12) << "Number of boxes sold" << endl;
inFile >> name >> numOfBoxesSold;
while(!inFile.eof())
{
outFile << left << setw(12)<< name << setw(12) << numOfBoxesSold << endl;
totalNumOfBoxesSold = numOfBoxesSold + totalNumOfBoxesSold;
inFile >> totalNumOfBoxesSold;
numOfVolunteers++;
inFile>> name >> numOfBoxesSold;
}
}
outFile << "The total number of boxes sold: " << totalNumOfBoxesSold << endl;
outFile << "The total money made by selling cookies: " << costOfoneBox * totalNumOfBoxesSold << endl;
if(numOfVolunteers!=0)
outFile << "The average number of boxes sold by each volunteer: " << totalNumOfBoxesSold / numOfVolunteers << endl;
else
cout << "No input" << endl;
outFile.close();
inFile.close();
return 0;
}
(I have a obviously two files associated with the program, inFile.txt has the information such as the cost of a box, and the names of volunteers followed by how many boxes they've sold, dataFile.txt is empty which I want to write in the total number of boxes sold, total amount of money made, and the average number of boxes sold per volunteer.)
you shouldn't be setting totalNumOfBoxesSold and you include a random entry of boxes before the while statement.
personally I would rather make it like if(inFile >> name >> numOfBoxesSold) instead of EOF, and keeping the the typed std helps you easily differ functions for formatting and actual values.
Thanks for the tip on adding color to the code, and I personally would rather do it that way as well, but this problem calls for an eof function so that's why I am trying to learn it
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
//Using standard library
usingnamespace std;
//Step into function main
int main()
{//Declare and initialize variables
string name;
int numOfVolunteers;
int numOfBoxesSold;
int totalNumOfBoxesSold;
double costOfoneBox;
totalNumOfBoxesSold=0;
numOfVolunteers=0;
cout << "This program outputs the amount of cookies sold and stores the information in a text file" << endl;
ifstream inFile;
ofstream outFile;
outFile.open("dataFile.txt");
inFile.open("inFile.txt");
if(!inFile)
{
cout << "File does not exist, program terminates." << endl;
return 1;
}
{outFile << fixed << showpoint << setprecision(2);//Output formatting to two decimal places
inFile >> costOfoneBox;//Read in from file inFile.txt the cost per box
inFile >> name >> numOfBoxesSold;//Read in from inFile.txt the name of the volunteer
while(inFile)//Begin while loop
{
numOfVolunteers++;//Increment the number of volunteers by one
totalNumOfBoxesSold = totalNumOfBoxesSold + numOfBoxesSold;//Calculate the number of boxes sold altogether.
inFile >> name;
inFile >> numOfBoxesSold;
outFile << name << numOfBoxesSold << endl;
}
outFile << "Cost of one box is: " << costOfoneBox << endl;
outFile << "The total number of boxes sold: " << totalNumOfBoxesSold << endl;
outFile << "The total revenue from sales is: " << costOfoneBox * totalNumOfBoxesSold << endl;
}
if(numOfVolunteers!=0)//
outFile << "The average number of boxes sold by each volunteer: " << totalNumOfBoxesSold / numOfVolunteers << endl;
else
cout << "No input" << endl;
outFile.close();
inFile.close();
return 0;
}//End function main
Be careful when using "eof" to control an input loop. You should pre-read the first "record" before the loop and the last thing you should do before the end of the loop is to read the next "record". You don't want to do any calculations until after you have tested the state of the stream at the beginning of the loop. Your output should be before you read the next "record" not before.
Using the actual "read" operation to control the is a much safer method of reading the data. By the way there is a difference between testing for eof() and just testing the stream state since eof() is only one of several causes of stream failures.
You could use a do-while and retrieve the value at the beginning of the loop, but you would have to check the stream after retrieving the values and in the do-while statement (which is cumbersome), if you read the link you would understand why. Overall I would be snarky and just post that link in a comment for your professor because you shouldn't be forced to learn flawed ways of coding.
While I agree that using eof() to control a read loop is problematic and is usually wrong, it is possible to use eof() to read a file properly when the proper care is taken. The problem is that you have to take those extra precautions that are usually not properly taken and those precautions are not necessary when using the actual read to control the loop. Another part of the problems is that eof() only tests for one error condition and some of the other causes of failures are far more common and need to be addressed.
By the way the OP is not actually using eof() in his code and is testing for all failure conditions so that is one step above using eof() for controlling a read loop.
Checking all the failbits gives the same side effect of checking eof (in a perfect environment), if the stream ends with a newline, its better but both are wrong.