初始化结构数组 C++ 时出现问题
Trouble Initializing an Array of Structures C++
我是 C++ 的新手,正在为 class:
解决一个问题
4. Annual Rainfall Report
Write a program that displays the name of each month in a year and its rainfall amount,
sorted in order of rainfall from highest to lowest. The program should use an array of
structures, where each structure holds the name of a month and its rainfall amount. Use a
constructor to set the month names. Make the program modular by calling on different
functions to input the rainfall amounts, to sort the data, and to display the data.
这是我目前的代码:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Month //defining the structure
{
string name;
double rain;
Month(string name = "", double rain = 0){} //constructor
};
const int SIZE = 12; //12 months
//initializing each structure with the name
Month month[SIZE] = { Month("January", 0), Month("February",0), Month("March", 0),
Month("April", 0), Month("May", 0), Month("June", 0),
Month("July", 0), Month("August", 0), Month("September", 0),
Month("October", 0), Month("November", 0), Month("December",0)};
void rainIn();
void sort();
void display();
int main() {
rainIn();
display();
return 0;
}
void rainIn()
{
for (int i = 0; i < SIZE; ++i)
{
cout << "Please enter the rainfall for " << month[i].name << ": ";
cin >> month[i].rain;
}
}
void sort() //will write later
{ }
void display()
{
for (int i = 0; i < SIZE; ++i)
{
cout << month[i].name << month[i].rain << endl;
}
}
我遇到的问题是当我尝试调用它时没有显示月份名称。我是否错误地初始化了数组?
看完评论和回答后,我开发了一个"Minimal, Complete, Verifiable"例子:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Month
{
string name;
double rain;
Month(string n = "", double r = 0) {}
};
Month month("January", 12);
int main() {
cout << month.name << " had " << month.rain << " inches of rain. " << endl;
return 0;
}
这仍然给了我同样的问题。我更改了构造函数(并添加了成员初始化列表),如下所示:
Month(string n = "", double r = 0) : name{n}, rain{r} {}
成功了。
问题不在于数组,而是构造函数实际上并未将成员变量设置为输入值。试试这个:
Month(string name = "", double rain = 0) : name{name}, rain{rain} {} //constructor
这个语法叫做"member initialization list"。如果您觉得它很陌生,请查看 this。
我是 C++ 的新手,正在为 class:
解决一个问题4. Annual Rainfall Report
Write a program that displays the name of each month in a year and its rainfall amount, sorted in order of rainfall from highest to lowest. The program should use an array of structures, where each structure holds the name of a month and its rainfall amount. Use a constructor to set the month names. Make the program modular by calling on different functions to input the rainfall amounts, to sort the data, and to display the data.
这是我目前的代码:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Month //defining the structure
{
string name;
double rain;
Month(string name = "", double rain = 0){} //constructor
};
const int SIZE = 12; //12 months
//initializing each structure with the name
Month month[SIZE] = { Month("January", 0), Month("February",0), Month("March", 0),
Month("April", 0), Month("May", 0), Month("June", 0),
Month("July", 0), Month("August", 0), Month("September", 0),
Month("October", 0), Month("November", 0), Month("December",0)};
void rainIn();
void sort();
void display();
int main() {
rainIn();
display();
return 0;
}
void rainIn()
{
for (int i = 0; i < SIZE; ++i)
{
cout << "Please enter the rainfall for " << month[i].name << ": ";
cin >> month[i].rain;
}
}
void sort() //will write later
{ }
void display()
{
for (int i = 0; i < SIZE; ++i)
{
cout << month[i].name << month[i].rain << endl;
}
}
我遇到的问题是当我尝试调用它时没有显示月份名称。我是否错误地初始化了数组?
看完评论和回答后,我开发了一个"Minimal, Complete, Verifiable"例子:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Month
{
string name;
double rain;
Month(string n = "", double r = 0) {}
};
Month month("January", 12);
int main() {
cout << month.name << " had " << month.rain << " inches of rain. " << endl;
return 0;
}
这仍然给了我同样的问题。我更改了构造函数(并添加了成员初始化列表),如下所示:
Month(string n = "", double r = 0) : name{n}, rain{r} {}
成功了。
问题不在于数组,而是构造函数实际上并未将成员变量设置为输入值。试试这个:
Month(string name = "", double rain = 0) : name{name}, rain{rain} {} //constructor
这个语法叫做"member initialization list"。如果您觉得它很陌生,请查看 this。