如何在月度销售费用统计场景中应用C++ Array?
Fixing Your C++ Sales/Expense Tracker with Arrays
Let's break down what's going wrong with your code and fix it step by step to properly use C++ arrays for your monthly sales, expense, and profit tracking.
Key Issues in Your Current Code
- Array Index Out-of-Bounds: Your outer loop uses
monthly <= 6, but your arrays (Month,sales,expenses) only have 6 elements (indices 0 to 5). Accessing index 6 will cause undefined behavior. - Unnecessary Nested Loops: The three inner
forloops serve no purpose—each only runs once and doesn't contribute to calculating or displaying data. - Incorrect Array Access: You’re trying to use integer variables like
Salesas array names (e.g.,Sales[sales]), which is invalid. You need to use your pre-defined arrays (sales,expenses) with themonthlyindex to get each month's values. - Manual Total Calculation: Asking the user to input totals defeats the purpose of the program. You should calculate these values automatically by iterating through the arrays.
Corrected Code
#include <iostream> #include <iomanip> // For formatting output neatly using namespace std; int main(){ // Array to store month names string Month[6] = {"January", "February", "March", "April", "May", "June"}; // Arrays for monthly sales and expenses float sales[6] = {1000, 1500, 2500, 5000, 4000, 1800}; float expenses[6] = {500, 250, 100, 1000, 200, 800}; // Variables to store totals float totalSales = 0, totalExpenses = 0, totalProfit = 0; cout << "Company Sales and Expenses For 2020" << endl; // Format output with fixed widths for readability cout << "\n" << left << setw(10) << "Month" << setw(10) << "Sales" << setw(12) << "Expenses" << setw(10) << "Profit" << endl; // Loop through each month (0 to 5) for (int monthly = 0; monthly < 6; monthly++){ // Calculate profit for the current month float profit = sales[monthly] - expenses[monthly]; // Display the month's data cout << left << setw(10) << Month[monthly] << setw(10) << sales[monthly] << setw(12) << expenses[monthly] << setw(10) << profit << endl; // Accumulate totals totalSales += sales[monthly]; totalExpenses += expenses[monthly]; totalProfit += profit; } // Display summary cout << "\n\tSummary:" << endl; cout << "\tTotal Sales: $" << totalSales << endl; cout << "\tTotal Expenses: $" << totalExpenses << endl; cout << "\tTotal Profit: $" << totalProfit << endl; return 0; }
Explanation of Improvements
- Fixed Loop Bound: Changed
monthly <=6tomonthly <6to avoid accessing invalid array indices. - Removed Redundant Loops: Got rid of the unnecessary inner loops and directly use the
monthlyindex to access each array's elements. - Proper Profit Calculation: Calculate each month's profit as
sales[monthly] - expenses[monthly]instead of trying to assign it via a loop. - Automated Total Calculation: Added accumulation logic inside the loop to compute totals automatically, so the user doesn't need to input them manually.
- Better Output Formatting: Used
<iomanip>andsetw()to align columns for cleaner, more readable output.
内容的提问来源于stack exchange,提问作者Cedric22
相关产品推荐
相关产品推荐

