C++ 降雨量统计C++;
我的任务是收集12个月的降雨量。我几乎完成了代码,但我不断收到以下错误消息:C++ 降雨量统计C++;,c++,visual-c++,C++,Visual C++,我的任务是收集12个月的降雨量。我几乎完成了代码,但我不断收到以下错误消息: “降雨”:未声明的标识符 'month':未声明的标识符 'Stats::getAvg':函数不接受0个参数 “降雨”:未声明的标识符 'month':未声明的标识符 “降雨”:未声明的标识符 'month':未声明的标识符 我不知道我做错了什么。以下是我目前掌握的代码: #include <iostream> #include <iomanip> #include <string>
“降雨”
:未声明的标识符
'month'
:未声明的标识符
'Stats::getAvg'
:函数不接受0个参数
“降雨”
:未声明的标识符
'month'
:未声明的标识符
“降雨”
:未声明的标识符
'month'
:未声明的标识符
我不知道我做错了什么。以下是我目前掌握的代码:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Stats
{
private:
double value;
double total;
double avg;
double largest;
double smallest;
double rainfall[12];
int NUM_MONTHS;
public:
void setValue(int, double); // set function
double getTotal(double[], int); // get functions
double getAvg(double[], int);
double getLargest(double[], int);
double getSmallest(double[], int);
};
void Stats::setValue(int month, double rain)
{
rainfall[month] = rain;
}
double Stats::getTotal(double array[], int month)
{
double total = 0.0;
for (int count = 0; count < month; count++)
total += rainfall[count];
return total;
}
double Stats::getLargest(double array[], int month)
{
double largest = rainfall[0];
for (int count = 0; count < month; count++)
{
if (rainfall[count] > largest)
largest = rainfall[count];
}
return largest;
}
double Stats::getSmallest(double array[], int month)
{
double smallest = rainfall[0];
for (int count = 0; count < month; count++)
{
if (rainfall[count] < smallest)
smallest = rainfall[count];
}
return smallest;
}
// Function prototype
void rainReport(Stats);
int main()
{
Stats rainData; // Create an instance of the Stats class
// to manage rainfall data
double rain;
const int NUM_MONTHS = 12; // Number of elements the array can hold
string months[NUM_MONTHS] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
for (int month = 0; month < NUM_MONTHS; month++)
{
cout << "Enter the rainfall (in inches) for month #";
cout << (month + 1) << ": ";
cin >> rain;
while (rain < 0)
{
cout << "Rainfall must be 0 or more. Please re-enter: ";
cin >> rain;
}
// Call class setValue function to store this month's rainfall
// value in the array.
rainData.setValue(month, rain);
}
// Call the rainReport function to produce a rain report.
// Pass it rainData, which is a Stats object.
rainReport(rainData);
return 0;
}
/************************************************************
* rainReport *
* Finds and returns the smallest value stored in the array.*
************************************************************/
void rainReport(Stats rainData)
{
// Display the total rainfall
cout << fixed << showpoint << setprecision(2) << endl;
cout << "Total rainfall for the year was ";
cout << setw(5) << rainData.getTotal(rainfall, month) << " inches." << endl;
// Display the average rainfall
cout << "Average rainfall for the year was ";
cout << setw(5) << rainData.getAvg() << " inches." << endl << endl;
// Display the largest & smallest amounts of monthly rain.
cout << "The largest amount of rainfall was ";
cout << setw(5) << rainData.getLargest(rainfall, month) << " inches.\n";
cout << "The smallest amount of rainfall was ";
cout << setw(5) << rainData.getSmallest(rainfall, month) << " inches.\n";
}
#包括
#包括
#包括
使用名称空间std;
班级统计
{
私人:
双重价值;
双倍总数;
双平均值;
两倍大;
双最小;
双倍降雨[12];
整数个月;
公众:
void setValue(int,double);//set函数
double getTotal(double[],int);//获取函数
双getAvg(双[],int);
double-getmax(double[],int);
double getminister(double[],int);
};
void Stats::setValue(整月,双倍降雨)
{
降雨量[月份]=降雨;
}
双统计::getTotal(双数组[],整数月)
{
双倍合计=0.0;
对于(int count=0;count最大值)
最大值=降雨量[计数];
}
回报最大;
}
双统计::GetMinimest(双数组[],整数月)
{
双最小=降雨量[0];
对于(int count=0;count coutrainReport
不是类Stats的成员。因此它不知道Stats中的所有内容
cout << setw(5) << rainData.getTotal(rainfall, month) << " inches." << endl;
这些错误消息中是否有行号。请查看每条错误消息,并查看它所指的行。这里有一个提示:问题都在rainReport
中。嗯,您没有说错误在哪行。我们不是通灵者。此外,问题可能是因为标识符未声明。不,问题是他没有引用Stat的私有成员。为了雪上加霜,他甚至没有使用传入的参数。即使没有私有函数问题,getAvg被声明为使用2个参数(没有默认值),但调用时它没有传递任何参数……并且函数体也没有定义。是的,我没有让它通过第一行麻烦。
double getTotal (int); // new definition
double Stats::getTotal (int month)
{
double total = 0.0;
for (int count = 0; count < month; count ++)
total += rainfall[count];
return total;
}