Программа работает и все выполняет должным образом, пока я не дохожу до той части, где функция рассчитывает годовые продажи и квартальные средние.Я не уверен, что проблема в функции или в отображении информации.
Показатель ежегодных продаж и среднеквартальных продаж составляет -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;
}