You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在月度销售费用统计场景中应用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 for loops 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 Sales as array names (e.g., Sales[sales]), which is invalid. You need to use your pre-defined arrays (sales, expenses) with the monthly index 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

  1. Fixed Loop Bound: Changed monthly <=6 to monthly <6 to avoid accessing invalid array indices.
  2. Removed Redundant Loops: Got rid of the unnecessary inner loops and directly use the monthly index to access each array's elements.
  3. Proper Profit Calculation: Calculate each month's profit as sales[monthly] - expenses[monthly] instead of trying to assign it via a loop.
  4. Automated Total Calculation: Added accumulation logic inside the loop to compute totals automatically, so the user doesn't need to input them manually.
  5. Better Output Formatting: Used <iomanip> and setw() to align columns for cleaner, more readable output.

内容的提问来源于stack exchange,提问作者Cedric22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:43:47