C++发票程序问题:customerInvoice函数失效及老年折扣需求
C++发票程序修复:函数调用错误与老年折扣实现
我正在收尾C++发票程序,尝试让customerInvoice函数正常输出,但调用该函数时出现错误,无法显示函数内的发票内容,其他变量输入均正常。同时需要实现老年折扣功能:当客户年龄≥60岁时,可享受购买金额5%的折扣。
原代码
#include <iostream> #include <iomanip> #include <string> using namespace std; const float flatTaxRate = 0.08; const float seniorDiscountRate = 0.05; int getIntValue (string toInput){ cout << "Please enter " << toInput <<": "; int value; cin >> value; return value; } string choiceOfItem(int choice) { return choice == 1 ? "Bread" : choice == 2 ? "Milk" : choice == 3 ? "Soap" : choice == 4 ? "Eggs" : choice == 5 ? "Deodorant" : choice == 6 ? "Juice" : choice == 7 ? "Chips" : choice == 8 ? "Forks" : choice == 9 ? "Spoons" : choice == 10 ? "Cups" : "Invalid Item: Select Again"; } int getIntInputValues (string toInput){ cout << "Please enter " << toInput <<": "; int value; cin >> value; return value; } double getDoubleInputValues (string toInput){ cout << "Please enter " << toInput <<": "; double value; cin >> value; return value; } bool customerAge(int age){ const int seniorCustomer = 60; return age >= seniorCustomer; } void customerInvoice(int choice, double price, int age) { cout << "----------------------------------- "; cout << " " << "Invoice" << " "; //can use \n as a tab if(seniorCustomer(age)) { price -= price * seniorDiscountRate; } cout << choiceOfItem(choice) <<":" << " " <<"$"<< price << endl; cout << "Tax:" << " " <<"$"<< price * flatTaxRate << endl; cout << "Discount:" << " " <<"$"<< "-" << price * seniorDiscountRate << endl; cout << "Total:" << " "<< "$" << price + (price * flatTaxRate) << endl; cout << "----------------------------------- "; } int main() { const int MIN_VALID = 1; const int MAX_VALID = 10; cout << "What would you like to buy?" << endl; for (int choice = MIN_VALID; choice <=MAX_VALID; ++choice) { cout << choice << ". " << choiceOfItem(choice)<< endl; } cout << endl; int choice = getIntValue("your choice"); while (choice < MIN_VALID || choice > MAX_VALID) { cout << "Sorry, " << choice << " wasn't a valid choice" << endl; cout << "Please try again:" << endl; choice = getIntValue("your choice"); } double price = getDoubleInputValues("price"); int age = getIntInputValues("age"); bool seniorCustomer; cout << " " << endl; customerInvoice; cout << choiceOfItem(choice) << endl; cout << price << endl; cout << age << endl; cout << seniorCustomer; return 0; }
核心问题修复
- 函数调用错误:
main中仅写customerInvoice不会执行函数,必须传入参数并加括号,正确写法为customerInvoice(choice, price, age); - 函数名误用:
customerInvoice中判断老年客户的函数名应为customerAge(age),而非seniorCustomer(age)——seniorCustomer是customerAge内部的常量,不是函数 - 折扣逻辑错误:原代码直接修改原价导致后续计算混乱,应先单独计算折扣金额,再基于原价计算税费和最终总价
- 字符串换行错误:C++不支持直接跨行写字符串,需将换行替换为
\n - 冗余变量:
main中未初始化的bool seniorCustomer无实际用途,直接删除
修复后的完整代码
#include <iostream> #include <iomanip> #include <string> using namespace std; const float flatTaxRate = 0.08; const float seniorDiscountRate = 0.05; int getIntValue(string toInput) { cout << "Please enter " << toInput << ": "; int value; cin >> value; return value; } string choiceOfItem(int choice) { return choice == 1 ? "Bread" : choice == 2 ? "Milk" : choice == 3 ? "Soap" : choice == 4 ? "Eggs" : choice == 5 ? "Deodorant" : choice == 6 ? "Juice" : choice == 7 ? "Chips" : choice == 8 ? "Forks" : choice == 9 ? "Spoons" : choice == 10 ? "Cups" : "Invalid Item: Select Again"; } // 合并重复的整数输入函数 int getIntInputValues(string toInput) { return getIntValue(toInput); } double getDoubleInputValues(string toInput) { cout << "Please enter " << toInput << ": "; double value; cin >> value; return value; } // 重命名函数,语义更清晰 bool isSeniorCustomer(int age) { const int seniorThreshold = 60; return age >= seniorThreshold; } void customerInvoice(int choice, double price, int age) { cout << "-----------------------------------\n"; cout << "\nInvoice\n"; double discount = 0.0; if (isSeniorCustomer(age)) { discount = price * seniorDiscountRate; } double tax = price * flatTaxRate; double total = price + tax - discount; // 设置金额输出格式,保留两位小数 cout << fixed << setprecision(2); cout << choiceOfItem(choice) << ": $" << price << endl; cout << "Tax: $" << tax << endl; cout << "Discount: $" << discount << endl; cout << "Total: $" << total << endl; cout << "-----------------------------------\n"; } int main() { const int MIN_VALID = 1; const int MAX_VALID = 10; cout << "What would you like to buy?" << endl; for (int i = MIN_VALID; i <= MAX_VALID; ++i) { cout << i << ". " << choiceOfItem(i) << endl; } cout << endl; int choice = getIntValue("your choice"); while (choice < MIN_VALID || choice > MAX_VALID) { cout << "Sorry, " << choice << " wasn't a valid choice" << endl; cout << "Please try again:" << endl; choice = getIntValue("your choice"); } double price = getDoubleInputValues("price"); int age = getIntInputValues("age"); cout << endl; customerInvoice(choice, price, age); return 0; }
额外优化说明
- 合并了重复的整数输入函数,减少冗余代码
- 重命名判断老年客户的函数,语义更直观
- 添加金额格式控制,确保输出保留两位小数,符合发票规范
- 调整计算逻辑,基于原价计算税费和折扣,避免数值混乱
内容的提问来源于stack exchange,提问作者Johng Jangus
相关产品推荐
相关产品推荐

