こんにちは私は、ユーザに変数を入力し、配列に時間を渡す気象ステーションのクラスに取り組んでいます。平均、高および低の値を計算します。私はそれを動作させるが、配列[要素]をプライベートにしたい。これは可能ですか?C++クラス内の配列要素の数を設定する
ここまでは私のコードです。助けをありがとうございました。
ブライアン
#include <iostream>
#include <iomanip>
using namespace std;
class WeatherStation
{
public:
WeatherStation();
void GetATemperatures(int[], int);
void DisplayATemperatures(int[], int);
void arrayCalcs(int[], int);
private:
static const int aTemps = 24;
static const int atemps[aTemps];
};
WeatherStation::WeatherStation()
{
int atemps[aTemps];
}
void WeatherStation::GetATemperatures(int atemps[], int aTemps)
{
for (int i = 0; i < aTemps; i++)
{
cout << "Please enter the temperature for " << i << ":00 ";
while(true)
{
cin >> atemps[i];
if(atemps[i] >= -50 && atemps[i] <= 130)
{
break;
} else {
cout << "This temperature is not valid\n";
cout << "Please enter a temperature between -50 and 130 degrees F \n";
cout << "Please enter a new temperature: ";
}
}
}
}
void WeatherStation::DisplayATemperatures(int atemps[], int aTemps)
{
cout << setw (5) << "Hour" << setw(24)<< "Temperature \n";
cout << "\n";
for (int k = 0; k < aTemps; k++)
{
cout << setw (3) << k << ":00" << setw (16) << atemps[k]<<endl;
}
cout <<"\n";
}
void WeatherStation::arrayCalcs(int atemps[], int aTemps)
{
int sumA = 0;
double average = 0.0;
int minA = atemps[0];
int maxA = atemps[0];
int lowest = 0;
int highest = 0;
//Sum of the AM temps
for (int kk = 0; kk < aTemps; kk++)
{
sumA = sumA + atemps[kk];
}
//calculation for average
average = sumA/aTemps;
//Figuring out the Min and Max AM temps
for (int MM = 0; MM < aTemps; MM++)
{
if(minA > atemps[MM])
{
minA = atemps[MM];
}
else if(maxA < atemps[MM])
{
maxA = atemps[MM];
}
lowest = minA;
highest = maxA;
}
//Display of the Calculation results
cout << "This is the average of todays temperatures: " << average <<endl;
cout <<endl;
cout << "Todays High temperature is: " << highest <<endl;
cout <<endl;
cout << "Todays Low temperature is: " << lowest <<endl;
}
int main()
{
cout <<"Welcome to the weather station.\n";
cout <<"Please enter Ferenheit temperatures for calculations: \n";
WeatherStation alpha;
alpha.GetATemperatures(atemps, aTemps);
alpha.DisplayATemperatures(temps, Temps);
alpha.arrayCalcs(temps,Temps);
cout << "\n";
system("pause");
return 0;
}
あなたのコードには、多くのローカル/グローバルな名前の競合があります。 C++コンパイラは、ローカルとグローバルが同じ名前を持つ場合、常にローカルを使用します。あなたがそれを認識しているかどうかはわかりません。いずれにせよ、それは単なる観測である。あなたの質問が本当にはっきりしないのですが、明確にしてください。 – littleadv
'aTemps'や' atemps'のように似ている変数名の使用は避けてください。 'aTemps'のより良い名前は' size'か 'length'でしょう。 –
明確にするために、すべての関数はarray-atemps []を使い、その配列の要素の数はaTempsとして定義され、24に等しくなります。 – Brian