-3

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);
}

6
  • 2
    Now seems like a very good time to learn how to use a debugger to step through your code line by line while monitoring variables and their values. That will help you understand exactly what the code is doing. Commented Jul 10 at 5:05
  • 1
    On a different note, please make sure that the input you pass to the program matches the output you're getting from your minimal reproducible example. And that the minimal reproducible example you show is the actual code producing the shown output. The input, output and code doesn't match for your question. Because of those inconsistencies, we can't tell exactly what the problem might be. Commented Jul 10 at 5:06
  • By the way, 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 into line when the break statement happen. So the getline(file, line); // Last line: Total price will fail. Commented Jul 10 at 5:15
  • 1
    read.csv seems like for R programming language and not suitable to C++. Read tag descriptions carefully when you pick them.
    – 3CxEZiVlQ
    Commented Jul 10 at 5:57
  • By the way, here's a hint: There are 7 columns, and there are 7 outputs of the total price. The code you show is far from a minimal reproducible example, and if we had the actual code generating the shown output it would have been very easy to see the problem. Commented Jul 10 at 6:24

0

Browse other questions tagged or ask your own question.