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
have you tried with totalbox += boxes; ?
Can you explain in detail what you mean?

You must zero your box count,
15 int totalbox=0;
And change this line to,
31 totalbox = totalbox + boxes;
Which is the same as,
totalbox += boxes;

Last edited on
Okay, when I tried that, it doesn't give me the results I need. It gives a 7 digit output to boxes and a 7 digit output to volunteers.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>

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

Last edited on
When I tried your code, it just kept giving me output of 0 repeatedly
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 ...

22 inFile.open("Data.txt");
Topic archived. No new replies allowed.