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.