I create a case 02 for calculate the bill. function is running but the problem is when the output is coming as a 0. that's problem in my code. Further Information's I cannot find where is the error coming from I changed variables also but it not correct that way. I think calculation is correct problem's is value is always shown in the console is 0.
我创建了一个用于计算账单的案例02。函数正在运行,但问题是当输出为0时。这就是我的代码中的问题。进一步的信息我找不到错误是从哪里来的,我也改变了变量,但不能以这种方式更正。我认为计算是正确的,问题的is值总是在控制台中显示为0。
This is the code,
这就是密码,
case 2: { // Create Bill
clearScreen();
cout << "****** CREATE BILL ******" << endl;
// Prompt user for customer name and invoice ID
string customerName;
string invoiceID;
cout << "Enter Customer Name: ";
cin.ignore();
getline(cin, customerName);
cout << "Enter Invoice ID: ";
cin >> invoiceID;
// Display menu for choosing bill type
clearScreen();
cout << "Choose Bill Type:" << endl;
cout << "1. Bakery Item Bill" << endl;
cout << "2. Package Bill" << endl;
cout << "\n****************" << endl;
cout << "Enter your choice: ";
int billType;
cin >> billType;
// Initialize variables to store bill details
double totalBill = 0.0;
string billContents = "";
if (billType == 1) { // Bakery Item Bill
clearScreen();
ifstream bakeryFile("bakery_items.txt");
if (!bakeryFile.is_open()) {
cout << "Data Base Error" << endl;
return;
}
vector<string> selectedItems;
vector<int> selectedQuantities;
string line;
while (getline(bakeryFile, line)) {
cout << line << endl;
}
bakeryFile.close();
// Prompt user to select items and quantities
char addMoreItems;
do {
string selectedItem;
int quantity;
cout << "\nEnter the Item ID of the bakery item you want to add: ";
cin >> selectedItem;
// Validate if the selected item exists
ifstream bakeryFile("bakery_items.txt");
string bakeryLine;
bool itemFound = false;
while (getline(bakeryFile, bakeryLine)) {
stringstream ss(bakeryLine);
string itemID, itemName, itemCategory;
double itemPrice;
ss >> itemID >> itemName >> itemCategory >> itemPrice;
if (itemID == selectedItem) {
itemFound = true;
cout << "Enter the quantity: ";
cin >> quantity;
// Validate if the quantity is valid
if (quantity <= 0) {
cout << "Please enter a positive quantity." << endl;
continue;
}
double subtotal = itemPrice * quantity;
totalBill += subtotal;
// Append bill details for the selected item to billContents
billContents += "Item ID: " + itemID + " | Item Name: " + itemName + " | Quantity: " + to_string(quantity) + " | Subtotal: " + to_string(subtotal) + " GBP\n";
break;
}
}
if (!itemFound) {
cout << "Please enter a valid Bakery Item ID." << endl;
}
cout << "Do you want to add more items? (Y/N): ";
cin >> addMoreItems;
} while (addMoreItems == 'Y' || addMoreItems == 'y');
}
else if (billType == 2) { // Package Bill
clearScreen();
ifstream packageFile("packages.txt");
if (!packageFile.is_open()) {
cout << "Data Base Error" << endl;
return;
}
// Initialize vector to store selected packages
vector<string> selectedPackages;
string line;
while (getline(packageFile, line)) {
cout << line << endl;
}
packageFile.close();
// Prompt user to select packages
char addMorePackages;
do {
string selectedPackage;
cout << "\nEnter the Package ID you want to add: ";
cin >> selectedPackage;
// Validate if the selected package exists
ifstream packageFile("packages.txt");
string packageLine;
bool packageFound = false;
while (getline(packageFile, packageLine)) {
stringstream ss(packageLine);
string packageID, packageName;
double packagePrice;
ss >> packageID >> packageName >> packagePrice;
if (packageID == selectedPackage) {
packageFound = true;
double subtotal = packagePrice;
totalBill += subtotal; // Add the subtotal to the totalBill
// Append bill details for the selected package to billContents
billContents += "Package ID: " + packageID + " | Package Name: " + packageName + " | Subtotal: " + to_string(subtotal) + " GBP\n";
break;
}
}
if (!packageFound) {
cout << "Please enter a valid Package ID." << endl;
}
cout << "Do you want to add more packages? (Y/N): ";
cin >> addMorePackages;
} while (addMorePackages == 'Y' || addMorePackages == 'y');
}
else {
cout << "Invalid choice. Exiting..." << endl;
return;
}
// Display the final bill
clearScreen();
cout << "\n****** FINAL BILL ******" << endl;
cout << "Customer Name: " << customerName << endl;
cout << "Invoice ID: " << invoiceID << endl;
cout << "Bill Type: " << (billType == 1 ? "Bakery Item Bill" : "Package Bill") << endl;
cout << "------------------------" << endl;
cout << billContents;
cout << "\n------------------------" << endl;
cout << "Total Bill (GBP): " << totalBill << endl;
// Ask the user if they want to save the bill
char saveBillChoice;
cout << "\nDo you want to save the bill? (Y/N): ";
cin >> saveBillChoice;
if (saveBillChoice == 'Y' || saveBillChoice == 'y') {
ofstream billFile("bills.txt", ios::app);
if (billFile.is_open()) {
billFile << "Customer Name: " << customerName << endl;
billFile << "Invoice ID: " << invoiceID << endl;
billFile << "Bill Type: " << (billType == 1 ? "Bakery Item Bill" : "Package Bill") << endl;
billFile << "------------------------" << endl;
billFile << billContents;
billFile << "Total Bill (GBP): " << totalBill << endl;
billFile << "========================" << endl;
billFile.close();
cout << "\n------------------------" << endl;
cout << "Bill saved successfully." << endl;
cout << "------------------------" << endl;
}
else {
cout << "Could not save the bill." << endl;
}
}
else {
cout << "\n[x] Bill not saved." << endl;
}
break;
}
I want to fix that calculation issue in this code.
我想修复此代码中的计算问题。
更多回答
Simplify the code, remove parts of it, until it starts working as expected. Perhaps even start over from an empty case 2: break
. Then add one very small piece of code, perhaps even just a single line. Build with extra warnings enabled, treated as errors. When it builds cleanly, test all possible inputs. Then add another very small piece of code. And so on. That will make it much easier to isolate the parts that causes a problem, and to debug it.
简化代码,删除其中的一部分,直到它开始按预期工作。也许甚至可以从一个空洞的案例2重新开始:突破。然后添加一小段代码,甚至可能只添加一行代码。生成时启用了额外警告,并将其视为错误。当它干净地构建时,测试所有可能的输入。然后添加另一小段代码。诸若此类。这将使隔离导致问题的部件并对其进行调试变得容易得多。
Speaking of making things simpler and easier, use functions. If you have multiple cases as big and complicated as this in your main
function, then that code will just be too complex, too hard to get a good overview of, too hard to debug and definitely too hard to maintain.
说到让事情变得更简单和更容易,使用函数。如果在主函数中有多个像这样大而复杂的用例,那么代码将太复杂,太难了解,太难调试,而且肯定太难维护。
我是一名优秀的程序员,十分优秀!