我已经设置了一个结构数组并将其传递给函数。我试图用输入填充该结构数组。我最初将输入设置为:
w->rainFall
,但是无法弄清楚它将如何填充其他下标。
我将其更改为:
w[counter].rainFall
认为这将在所有数组中正确循环,但是输出数据未提供预期的结果。
有人可以帮助我找出从函数内部正确填充此结构吗?
完整代码在这里
#include "pch.h"
#include <iostream>
using namespace std;
struct Weather
{
int rainFall;
int highTemp;
int lowTemp;
double averageTemp;
};
enum Months
{
January, February, March, April, May, June, July, August, September, October, November, December
};
void showData(struct Weather* w, int);
void getData(struct Weather* w, int);
void displayMonthName(Months);
int main()
{
const int SIZE = December+1;
struct Weather w[SIZE];
Months thisMonth;
cout << "Enter The Following Information: " << endl;
for (thisMonth = January; thisMonth <= December; thisMonth = static_cast<Months>(thisMonth +1))
{
int counter = 0;
displayMonthName(thisMonth);
cout << " | Month "<< thisMonth+1 << " " << endl;
getData(w, counter);
counter++;
}
showData(w, SIZE);
}
void showData(struct Weather* w, int SIZE)
{
for (int i = 0; i <= SIZE; i++)
{
cout << w[i].rainFall << endl;
cout << w[i].highTemp << endl;
cout << w[i].lowTemp << endl << endl;
}
}
void getData(struct Weather* w, int counter)
{
cout << " Total Rainfall: ";
cin >> w[counter].rainFall;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
while (!cin || w->rainFall < 0)
{
cout << "Rainfall cannot be less than 0 inches" << endl;
cout << " Total Rainfall: ";
cin >> w[counter].rainFall;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << " High Temperature: ";
cin >> w[counter].highTemp;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
while (!cin || w->highTemp <-100 || w->highTemp >140)
{
cout << "High Temperature must be between -100 & +140" << endl;
cout << " High Temperature: ";
cin >> w[counter].highTemp;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << " Low Temperature :";
cin >> w[counter].lowTemp;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
while (!cin || w->lowTemp > w->highTemp || w->lowTemp < -100 || w->lowTemp >140)
{
cout << "Low Temperature must be between -100 & +140 while also being lower than the High Temperature" << endl;
cout << endl << " Low Temperature :";
cin >> w[counter].lowTemp;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
void displayMonthName(Months m)
{
switch (m)
{
case January:
cout << "January";
break;
case February:
cout << "February";
break;
case March:
cout << "March";
break;
case April:
cout << "April";
break;
case May:
cout << "May";
break;
case June:
cout << "June";
break;
case July:
cout << "July";
break;
case August:
cout << "August";
break;
case September:
cout << "September";
break;
case October:
cout << "October";
break;
case November:
cout << "November";
break;
case December:
cout << "December";
break;
}
}
答案 0 :(得分:1)
首先,int counter
应该在for循环之外,否则您将其重新初始化为0并每次将0传递给getData函数。
int counter = 0; // Should be here.
for (thisMonth = January; thisMonth <= December; thisMonth = static_cast<Months>(thisMonth +1))
{
//Not here
displayMonthName(thisMonth);
cout << " | Month "<< thisMonth+1 << " " << endl;
getData(w, counter);
counter++;
}
接下来,您要遍历数组1次。将<=
更改为<
。
void showData(struct Weather* w, int SIZE)
{
for (int i = 0; i < SIZE; i++)
{
cout << w[i].rainFall << endl;
cout << w[i].highTemp << endl;
cout << w[i].lowTemp << endl << endl;
}
}