This is the data in my csv file
4
09-07-2024
Thnol Coffee,Iced,Large,10000,No Sweet,Normal Ice,2
Thnol Coffee,Hot,Small,8500,More Sweet,Normal Ice,1
Latte,Frappe,Large,12500,Normal Sweet,Less Ice,2
53500
And this is from printing my code
Invoice: 8
Date: 10-07-2024
Latte Iced Large 10000 Normal Sweet Normal Ice 2
Cappuccino Frappe Large 12500 Less Sweet Normal Ice 2
45000 45000 45000 45000 45000 45000 45000
Total Price: 45000 KHR
The code print the total price many times
I use vector to print it
void printCoffeeUser(ListUser* ls, ListCoffee* cf){
int totalPrice;
int inv; // invoice number
string date;
string filePath = base_path + "/Receipt" + to_string(invoice) + ".csv";
ifstream file;
file.open(filePath, ios::in);
if (!file.is_open()) {
cerr << "Error opening file: " << filePath << endl;
return;
}
string line;
getline(file, line); // First line: Invoice number
inv = stoi(line);
getline(file, line); // Second line: Date
date = line;
vector<Coffee> orders;
while (getline(file, line)) {
istringstream ss(line);
string token;
getline(ss, token, ',');
if (token.empty()) break; // Handle the total price line
Coffee order;
order.name = token;
getline(ss, token, ',');
order.type = token;
getline(ss, token, ',');
order.sizes = token;
getline(ss, token, ',');
order.price = stoi(token);
getline(ss, token, ',');
order.sugar = token;
getline(ss, token, ',');
order.ice = token;
getline(ss, token, ',');
order.quantity = stoi(token);
orders.push_back(order);
}
getline(file, line); // Last line: Total price
totalPrice = stoi(line);
file.close();
// Print the invoice
cout << "Invoice: " << inv << '\n';
cout << "Date: " << date << '\n';
cout << "Coffee\tType\tSize\tPrice\tSugarlvl\tIcelvl\tquant\n";
for (const auto& order : orders) {
cout << order.name << '\t' << order.type << '\t' << order.sizes << '\t' << order.price << '\t' << order.sugar << '\t' << order.ice << '\t' << order.quantity << '\n';
}
cout << "Total Price: " << totalPrice << " KHR\n";
system("Pause");
return userHomePage(ls, cf);
}
if (token.empty()) break; // Handle the total price line
will not work. After the loop you attempt to read the total price line, but it has already been read intoline
when thebreak
statement happen. So thegetline(file, line); // Last line: Total price
will fail.