Working through C++ Primer Plus and am trying to cin data to a dynamically allocated array of structures. One of the items is a char array. How do I write to these struct members? Posting code of my wrong attempt so you can see what I'm trying to do.
使用C ++ Primer Plus并尝试将数据cin到动态分配的结构数组。其中一个项目是char数组。我如何写这些结构成员?发布我错误尝试的代码,以便您可以看到我正在尝试做的事情。
#include <iostream>
using namespace std;
struct contributions
{
char name[20];
double dollars;
};
int donors;
int main()
{
cout << "How many contributors will there be?\n";
cin >> donors;
contributions * ptr = new contributions[donors];
for(int i = 0; i <= donors; i++)
{
cout << "Enter donor name #" << i+1 << ": \n";
cin >> ptr->contributions[i].name;
cout << "Enter donation amount: \n";
cin >> ptr->contributions[i].dollars;
}
Thanks in advance!
提前致谢!
4 个解决方案
#1
Try using std::string instead of char[20] for name and the sample should work just fine.
尝试使用std :: string而不是char [20]作为名称,样本应该可以正常工作。
struct contributions
{
std::string name;
double dollars;
};
also change the access to
也改变了访问权限
ptr[i].name
#2
cin >> ptr[i].name;
ptr
is the name of the variable, it is of type contributions*
. It is an array of contributions
, so to access the ith member, use ptr[i]
. Then access the name
field of that member via ptr[i].name
. Also, cin >> char[]
may not work (I don't recall for sure), as char[]
is more of a C-ish thing, whereas cin
is C++. So you might need to change the type of name
to std::string.
ptr是变量的名称,它的类型为贡献*。这是一个贡献数组,所以要访问第i个成员,请使用ptr [i]。然后通过ptr [i] .name访问该成员的名称字段。此外,cin >> char []可能不起作用(我不记得肯定),因为char []更像是一个C-ish的东西,而cin是C ++。因此,您可能需要将名称类型更改为std :: string。
As an aside, convention is to name your structs/classes with a singular noun. Thus contribution
would be a more correct name; every instance represents a single contribution.
顺便说一下,惯例是用单数名词命名你的结构/类。因此,贡献将是一个更正确的名称;每个实例代表一个贡献。
#3
cin >> ptr[i].name;
(the correct form) would stop at the first whitespace character (and risk a buffer overflow if no such character comes before the 20 spaces in the array are exhausted). Use cin.getline(ptr[i].name, 20)
instead.
cin >> ptr [i] .name; (正确的形式)将停在第一个空格字符处(如果在数组中的20个空格用尽之前没有这样的字符,则存在缓冲区溢出的风险)。请改用cin.getline(ptr [i] .name,20)。
#4
Also, using a std::vector of contributions w2ill make the code much simpler. as it is, you have a memory leak. If this is direct from C++ Primer Plus I'd seriously suggest changing to a text book that will teach you modern, correct C++, such as Accelerated C++ by Koenig & Moo.
此外,使用std :: vector of w2ill使代码更加简单。事实上,你有内存泄漏。如果这是直接来自C ++ Primer Plus,我会认真地建议改为教你现代正确的C ++的教科书,例如Koenig&Moo的Accelerated C ++。
#1
Try using std::string instead of char[20] for name and the sample should work just fine.
尝试使用std :: string而不是char [20]作为名称,样本应该可以正常工作。
struct contributions
{
std::string name;
double dollars;
};
also change the access to
也改变了访问权限
ptr[i].name
#2
cin >> ptr[i].name;
ptr
is the name of the variable, it is of type contributions*
. It is an array of contributions
, so to access the ith member, use ptr[i]
. Then access the name
field of that member via ptr[i].name
. Also, cin >> char[]
may not work (I don't recall for sure), as char[]
is more of a C-ish thing, whereas cin
is C++. So you might need to change the type of name
to std::string.
ptr是变量的名称,它的类型为贡献*。这是一个贡献数组,所以要访问第i个成员,请使用ptr [i]。然后通过ptr [i] .name访问该成员的名称字段。此外,cin >> char []可能不起作用(我不记得肯定),因为char []更像是一个C-ish的东西,而cin是C ++。因此,您可能需要将名称类型更改为std :: string。
As an aside, convention is to name your structs/classes with a singular noun. Thus contribution
would be a more correct name; every instance represents a single contribution.
顺便说一下,惯例是用单数名词命名你的结构/类。因此,贡献将是一个更正确的名称;每个实例代表一个贡献。
#3
cin >> ptr[i].name;
(the correct form) would stop at the first whitespace character (and risk a buffer overflow if no such character comes before the 20 spaces in the array are exhausted). Use cin.getline(ptr[i].name, 20)
instead.
cin >> ptr [i] .name; (正确的形式)将停在第一个空格字符处(如果在数组中的20个空格用尽之前没有这样的字符,则存在缓冲区溢出的风险)。请改用cin.getline(ptr [i] .name,20)。
#4
Also, using a std::vector of contributions w2ill make the code much simpler. as it is, you have a memory leak. If this is direct from C++ Primer Plus I'd seriously suggest changing to a text book that will teach you modern, correct C++, such as Accelerated C++ by Koenig & Moo.
此外,使用std :: vector of w2ill使代码更加简单。事实上,你有内存泄漏。如果这是直接来自C ++ Primer Plus,我会认真地建议改为教你现代正确的C ++的教科书,例如Koenig&Moo的Accelerated C ++。