Reading in from a file

Hello cplusplus users!

I am trying to read from a file and display the contents as output.

My code is as follows:

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

using namespace std;

int main()
{
string name;
double cost;
int boxes;
int numvolunteer;
int totalbox;

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 =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
this is not safe since you have not keep the previous value in boxes

1
2
3
4
5
6
7
while (!inFile.eof())
{
inFile >> name >> boxes;
totalbox =boxes + boxes;
numvolunteer++;
cout << name << " " << boxes << endl;
}


you should also close the file after using it .
How would I do this better? Also when I add in inFile.close I get an error stating its an overloaded function.


13 I:\Class stuff\Programming\Tests\experiment.cpp:41 statement cannot resolve address of overloaded function
?hmm dunno , ok nevermind for the close()

1
2
3
4
5
6
int totalboxes = 0;

//..

inFile >> name >> boxes;
totalbox+= boxes;
Topic archived. No new replies allowed.