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

Posted

技术标签:

【中文标题】没有语法错误,但我的程序显示奇怪的数字【英文标题】:No syntax errors but my program displays weird numbers 【发布时间】:2018-12-04 02:34:47 【问题描述】:

程序正常工作并正确执行所有内容,直到我到达函数计算年销售额和季度平均值的部分。我不确定问题出在功能上还是信息显示上。

年销售额和平均季度销售额显示为 -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;

【问题讨论】:

你永远不会在任何地方打电话给calculateAnnualSalescalculateQuarterlyAvg new Company[corporateSize]; 更改为new Company[corporateSize](); 至少意味着它们被初始化为0.0 而不是垃圾值 哦!好的谢谢!我把 calculateAnnualSales(divisions[i]);在我的 void ReadCorpSales 中,它开始工作。你能解释一下原因吗? 这其中哪一部分需要解释?除了忘记调用的函数之外,您不会将 annualSalesavgQSales 初始化为任何函数中的任何值。 另见:What is a debugger and how can it help me diagnose problems? 【参考方案1】:

关于垃圾值,或许可以参考this或this。 其他的,M.M的回答涵盖了这个问题

【讨论】:

以上是关于没有语法错误,但我的程序显示奇怪的数字的主要内容,如果未能解决你的问题,请参考以下文章

无法在点击事件时设置离子本地通知(奇怪的语法错误)

我没有使用计数,但我的日志告诉我“计数语法错误”

Python 2.7.3 中的奇怪语法错误

如果我的 html/javascript/css 计算器上的数字不兼容,如何添加“语法错误”?

清单:行:1,列:1,Chrome 浏览器上的语法错误

Rails Jcrop 语法错误