Adding ints together from an input file

I have a code which looks like this

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
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

struct Bill{
    std::string name;
    int bill_value;
};

enum Status{abnorm, norm};

bool read(std::ifstream &f, Bill &e, Status &st);


int main()
{
    std::ifstream x("inp.txt");
    if (x.fail() ) {
        std::cout << "Error!\n";
        return 1;
    }

    Bill dx;
    Status sx;
    int s = 0;
    while(read(x,dx,sx)) {
        s += dx.bill_value;
    }

    std::cout << "Today income: " << s << std::endl;
    return 0;
}

bool read(std::ifstream &f, Bill &e, Status &st){
    std::string line;
    getline(f,line);
    if (!f.fail() && line!="") {
        st = norm;
        std::istringstream in(line);
        in >> e.name;

        std::string product;
        int value;
        e.bill_value= 0;
        while( in >> product >> value) e.bill_value+= value;
    }
    else st=abnorm;

    return norm==st;
}


The input file called inp.txt and looks like this:

Joe tv 1200 mouse 50000
Peter glass 8000
Harry mouse 8200 usb 8000 headphones 98900
David book 500 800 mouspad 900
Liam phone 8000 cooler 3000 headphones 3000
Daniel laptop 700 pot 9000

The first is always the name of the customer, which follows by the product(s) he bought and its value.

For example Peter bought a glass for 8000, but David bought 2 books in two differente price.

And this is where my problem comes, because at David's line the program only return the value of the first book, and not the sum of the line, and i would like to know how much profit the shop made so i would need to calculate the sum of David's bill also.
Last edited on
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

You would not have a problem, if the input were:
David book 500 book 800 mouspad 900

or
David; book 500 800; mouspad 900


As is, you have to look at each word following a product name in the input and figure out whether it is number or text.
If number, it is another item/price of current product.
If text, it is the name of the the next product.

In other words, you cannot while ( in >> product >> value ) because "800" is not a product and "mouspad" is not a value.
Hello INeedSumCode,

I have found this to be helpful. http://www.cplusplus.com/articles/z13hAqkS/

Another idea would be to stay with the pattern name product price, i.e., have "Davis's" line look like "Liam's" line. Then reading from the string stream there is no problem with how many pairs follow the name.

I see that you have included the file "vector". Maybe after reading the file you might want to create a vector of structs to work with.

I made some changes to the program. This is a suggestion of what you could do.
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <fstream>
#include <sstream>
//#include <vector>  // <--- Not used yet.

struct Bill
{
	//std::string name;  // <--- You may want to add this to the struct.
	std::string productName;
	int bill_value;
};

enum Status { abnorm, norm };

bool read(std::ifstream &inFile, Bill &bill/*, Status &status*/);  // <--- Changed.

int main()
{
	std::ifstream inputFile("Sales.txt");

	if (!inputFile)  // <--- All you need.
	{
		std::cerr << "\n    File\" Sales.txt\" did not open!" << std::endl;  // <--- Changed. Used "cerr".

		return 1;
	}

	Bill bill{};
	Status status{};

	int income = 0;

	while (read(inputFile, bill/*, status*/))  // <--- Changed.
	{
		income += bill.bill_value;
	}

	std::cout << "Today income: " << income << std::endl;

	return 0;
}

bool read(std::ifstream &inFile, Bill &bill/*, Status &status*/)  // <--- Changed.
{
	Status status;  // <--- Moved.
	int value{};
	std::string line, name, product;
	status = abnorm;

	//getline(inFile, line);

	//if (!inFile.fail() && line != "")
	//{
	//	statis = norm;

	//	std::istringstream in(line);

	//	in >> bill.name;

	//	std::string product;

	//	int value;

	//	bill.bill_value = 0;

	//	while (in >> product >> value) bill.bill_value += value;
	//}
	//else statis = abnorm;

	while (std::getline(inFile, line))
	{
		status = norm;

		std::istringstream in(line);

		in >> name;

		while (in >> product >> value)  // <--- After reading "name" input file needs to match this pattern for the rest of the line.
			bill.bill_value += value;
	}

	return status;  // <--- Changed.
}

If the comments do not explain it and you have questions let me know.

What I want you to notice is the different variable names and how they explain what they are for.

In the function I left what you started with so you could see the difference.

Andy

Edit:
The input file I used looks like this:

Joe tv 1200 mouse 50000
Peter glass 8000
Harry mouse 8200 usb 8000 headphones 98900
David book 500 book 800 mouspad 900
Liam phone 8000 cooler 3000 headphones 3000
Daniel laptop 700 pot 9000

Last edited on
Topic archived. No new replies allowed.