我正在开发一个程序,用于读取天气数据并将统计信息报告给用户。如果我在main中运行所有内容(不使用类),我知道我的程序完成了我需要它做的事情。我通过使用对象调用它来访问我的display()
函数时遇到问题。
我对这一切都很陌生,所以如果有人能帮我理解我的错误在哪里,我真的很感激任何帮助。谢谢!
#include<iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
class Weather
{
private:
static const int NUM_MONTHS = 3,
DAYS_IN_MONTH = 30;
const string name[NUM_MONTHS] = { "June", "July", "August" };
char dayType[NUM_MONTHS][DAYS_IN_MONTH];
int rainy; // Counts number of days of each type
int cloudy; // during one particular month
int sunny;
int rainyTotal = 0; // Counts number of days of each type
int cloudyTotal = 0; // during 3-month period
int sunnyTotal = 0;
int wettestMonth; // Index of rainiest month
int wettestMonthsRain = -1; // Number of rainy days in rainiest month
int cloudiestMonth;
int cloudiestMonthClouds = -1;
int sunniestMonth;
int sunniestMonthSun = -1;
public:
void readFileData(char[][DAYS_IN_MONTH]);
void display();
};
Weather::Weather()
{
name: { "June", "July", "August"; }
}
void Weather::readFileData(char dayType[][DAYS_IN_MONTH])
{
ifstream weatherData;
//Open data file
weatherData.open("weather.txt");
if (!weatherData)
{
cout << "Could not open the data file.\n";
}
// Else, if file was opened correctly, read weather data into the array
for (int month = 0; month < NUM_MONTHS; month++)
{
for (int day = 0; day < DAYS_IN_MONTH; day++)
weatherData >> dayType[month][day];
}
// Close the data file
weatherData.close();
}
void Weather::display()
{
// Print report heading
cout << " Summer Weather Report\n\n"
<< "Month Rainy Cloudy Sunny\n"
<< "_____________________________\n";
// Accumulate and display weather statistics
for (int month = 0; month < NUM_MONTHS; month++)
{
// Reset accumulators
rainy = cloudy = sunny = 0;
// Accumulate weather statistics for current month
for (int day = 0; day < DAYS_IN_MONTH; day++)
{
if (dayType[month][day] == 'R')
rainy++;
else if (dayType[month][day] == 'C')
cloudy++;
else
sunny++;
}
// Add monthly totals to grand totals
rainyTotal += rainy;
cloudyTotal += cloudy;
sunnyTotal += sunny;
// Determine if this month is the rainiest so far
if (rainy > wettestMonthsRain)
{
wettestMonthsRain = rainy;
wettestMonth = month;
}
// check if this month is the cloudiest month
if (cloudy > cloudiestMonthClouds)
{
cloudiestMonthClouds = cloudy;
cloudiestMonth = month;
}
//check if this month is the sunniest month
if (sunny > sunniestMonthSun)
{
sunniestMonthSun = sunny;
sunniestMonth = month;
}
{
}
// Display this month's results
cout << left << setw(6) << name[month]
<< right << setw(6) << rainy << setw(8) << cloudy
<< setw(7) << sunny << endl;
}
// Display summary data
cout << "_____________________________\n";
cout << "Totals" << setw(6) << rainyTotal << setw(8)
<< cloudyTotal << setw(7) << sunnyTotal << endl << endl;
cout << "The month with the most rainy days was "
<< name[wettestMonth] << ".\n";
cout << "The month with the most cloudy days was "
<< name[cloudiestMonth] << ".\n";
cout << "The month with the most sunny days was "
<< name[sunniestMonth] << ".\n";
}
int main()
{
//create object
Weather summer;
summer.display();
return 0;
}
在我解决问题时进行编辑。添加了构造函数,允许我的程序编译,但现在我的几个月的总数都显示为零。非常感谢任何帮助!