没有语法错误,但是我的程序显示奇怪的数字

时间:2018-12-04 02:34:47

标签: c++ visual-studio visual-c++

程序可以正常工作并执行所有操作,直到我到达该函数计算“年度销售额”和“季度平均值”的部分为止。我不确定问题是在功能中还是在信息显示中。

“年销售额和平均季度销售额”显示为-6.27744e + 66

我觉得我缺少明显的东西。

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct Company
{
    string dName;                                                           // Stores Division Name
    double firstQSales;                                                     // First Quarterly Sales
    double sndQSales;                                                       // Second Quarterly Sales
    double thirdQSales;                                                     // Third Quarterly Sales
    double fourthQSales;                                                    // Fourth Quarterly Sales
    double annualSales;                                                     // Annual Quarterly Sales
    double avgQSales;                                                       // Average Quarterly Sales
};

const double NUM_OF_QUARTERS = 4;

void readCorpSales(Company*, int);
void displaySalesData(Company*, int);
void calculateAnnualSales(Company&);
void calculateQuarterlyAvg(Company&);

int main()
{
    int corporateSize;

    cout << "How many divisions does your corporate has: ";
    cin >> corporateSize;
    cin.ignore();

    while (corporateSize < 0)
    {
        cout << "Please enter a positive number: ";
        cin >> corporateSize;
    }

    Company* divisions = new Company[corporateSize];
    readCorpSales(divisions, corporateSize);
    displaySalesData(divisions, corporateSize);

    delete[] divisions;

    system("pause");
    return 0;
}

void readCorpSales(Company *divisions, int a)
{
    for (int i = 0; i < a; ++i)
    {
        cout << "Enter division's name: ";
        cin >> divisions[i].dName;

        cout << "Enter 1st quarter sales: ";
        cin >> divisions[i].firstQSales;

        cout << "Enter 2nd quarter sales: ";
        cin >> divisions[i].sndQSales;

        cout << "Enter 3rd quarter sales: ";
        cin >> divisions[i].thirdQSales;

        cout << "Enter 4th quarter sales: ";
        cin >> divisions[i].fourthQSales;

        cout << "\n";
    }
}

void displaySalesData(Company* divisions, int a)
{
    cout << "\n===============================";
    cout << "\n Corporate Data Sales Report";
    cout << "\n===============================\n";
    cout << "\nDivision Name";
    cout << "\t 1st Q ($)";
    cout << "\t 2nd Q ($)";
    cout << "\t 3rd Q ($)";
    cout << "\t 4th Q ($)";
    cout << "\t Annual Sales ($)";
    cout << "\t Avg Q Sales ($)\n";

    for (int i = 0; i < a; ++i)
    {
        cout << divisions[i].dName << "\t";
        cout << divisions[i].firstQSales << "\t";
        cout << divisions[i].sndQSales << "\t";
        cout << divisions[i].thirdQSales << "\t";
        cout << divisions[i].fourthQSales << "\t";
        cout << divisions[i].annualSales << "\t";
        cout << divisions[i].avgQSales << "\n";
    }

}

void calculateAnnualSales(Company &divisions)
{
    divisions.annualSales = divisions.firstQSales + divisions.sndQSales + divisions.thirdQSales + divisions.fourthQSales;
}

void calculateQuarterlyAvg(Company &divisions)
{
    divisions.avgQSales = divisions.annualSales / 4;
}

1 个答案:

答案 0 :(得分:0)

关于垃圾值,也许您可​​以参考thisthis。 另外,M.M的答案涵盖了这个问题