cout << "What is the cost of each box? ";
cin >> cost;
cout << endl;
while (!inFile.eof())
{
inFile >> name >> boxes;
totalbox =boxes + boxes;
numvolunteer++;
cout << name << " " << boxes << endl;
}
cout << "Name: " << name << endl;
cout << "The number of boxes sold is: " << totalbox << endl;
cout << "The cost of each box sold is: " << cost << endl;
cout << "The number of volunteers is: " << numvolunteer << endl;
system ("Pause");
return 0;
}
When I do the output, I get:
What is the cost of each box? 1.25
Sara 120
Lisa 128
Cindy 359
Nicole 267
Blair 165
Abby 290
Amy 190
Megan 450
Elizabeth 280
Meredith 290
Leslie 430
Chelsea 378
Name: Chelsea
The number of boxes sold is: 756
The cost of each box sold is: 1.25
The number of volunteers is: 4284934
Press any key to continue . . .
______
Can you guys tell me where I am going wrong? I can even give the format and text in the Data.txt file below:
Sara 120
Lisa 128
Cindy 359
Nicole 267
Blair 165
Abby 290
Amy 190
Megan 450
Elizabeth 280
Meredith 290
Leslie 430
Chelsea 378
Strange it works for me.
Remember to initialize your variables otherwise they start with a random number which is why totalbox and numVolunteer must be initialized to zero before adding things to them.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
usingnamespace std;
int main()
{
string name;
double cost=0;
int boxes=0;
int numvolunteer=0;
int totalbox=0;
cout << fixed << showpoint;
cout << setprecision(2);
ifstream inFile;
inFile.open("Data.txt");
cout << "What is the cost of each box? ";
cin >> cost;
cout << endl;
while (!inFile.eof())
{
inFile >> name >> boxes;
totalbox = totalbox + boxes;
numvolunteer++;
cout << name << " " << boxes << endl;
}
cout << "Name: " << name << endl;
cout << "The number of boxes sold is: " << totalbox << endl;
cout << "The cost of each box sold is: " << cost << endl;
cout << "The number of volunteers is: " << numvolunteer << endl;
// system ("Pause");
return 0;
}
Perhaps it was the file path on line22
Make sure it is to the location of your Data.txt file.
I left my path in the source code but have just changed it above to ...