在 C++ 中使用字符指针将字符串保存到 class 属性
Saving string to class attribute using character pointer in C++
我在尝试使用 cin 获取输入,将它们保存到几个变量,然后构建一个 class 时遇到困难。当我输入第一个输入时,程序没有等待下一个输入,而是无限循环并不断显示提示。
class是:
class person {
public:
char *name;
char *email;
int phone;
// constructor that uses the parameters to initialize the class properties
person(char *cName, char *cEmail, int iPhone) {
name = new (char[32]); // acquiring memory for storing name
email = new (char[32]); // acquiring memory for storing email
strcpy(name, cName); // initialize name
strcpy(email, cEmail); // initialize email
phone = iPhone; // initialize phone
}
virtual ~person() {
delete[] name;
delete[] email;
}
};
输入和构造函数调用如下:
char* nameIn = new (char[32]); // acquiring memory for storing name
char* emailIn = new (char[32]);
int iPhoneIn;
cout << "Enter name, email, and phone:\n";
cin >> *nameIn;
cin >> *emailIn;
cin >> iPhoneIn;
person* newPerson = new person(nameIn, emailIn, iPhoneIn); //create node
您可以 fix/improve 您的代码有几种方式。
首先,我们将从明显错误的地方开始。当您从 cin 读取输入时,不要取消引用(调用 * 运算符)您的字符指针 (char*)。 cin 应该收到 char*,而不是 char。如果你给它传递一个 char,它只会写你输入的第一个字符。因此,如果要输入字符串,则需要将 char* 传递给它。
cin >> *nameIn; // this should be: cin >> nameIn;
cin >> *emailIn; // this should be cin >> emailIn;
运行 这个片段有和没有建议的变化看看我的意思:
#include <iostream>
using namespace std;
int main() {
cout << "Input a string: ";
char * c = new(char[200]);
cin >> *c; // only writes the first input character. Change to: cin >> c;
cout << "You input: " << c; // prints the first input character then garbage
delete[](c);
return 0;
}
其次,不要用整数表示 phone 数字。 int phone;
不适用于标准的 10 位 phone 号码。在大多数平台上,可以用整数表示的最大值约为 21 亿。大多数 10 位数字都大于 21 亿。请改用字符数组。相应地调整您的输入法。
第三,不要不必要地动态分配内存。如果您从一开始就知道所有名称都适合 32 个字符,那么就这样声明它们。
char *name; // change to char name[32];
char *email; // change to char email[32];
这会 运行 更快,并且让您免于必须释放已分配内存的痛苦。您将能够完全删除析构函数。
我在尝试使用 cin 获取输入,将它们保存到几个变量,然后构建一个 class 时遇到困难。当我输入第一个输入时,程序没有等待下一个输入,而是无限循环并不断显示提示。
class是:
class person {
public:
char *name;
char *email;
int phone;
// constructor that uses the parameters to initialize the class properties
person(char *cName, char *cEmail, int iPhone) {
name = new (char[32]); // acquiring memory for storing name
email = new (char[32]); // acquiring memory for storing email
strcpy(name, cName); // initialize name
strcpy(email, cEmail); // initialize email
phone = iPhone; // initialize phone
}
virtual ~person() {
delete[] name;
delete[] email;
}
};
输入和构造函数调用如下:
char* nameIn = new (char[32]); // acquiring memory for storing name
char* emailIn = new (char[32]);
int iPhoneIn;
cout << "Enter name, email, and phone:\n";
cin >> *nameIn;
cin >> *emailIn;
cin >> iPhoneIn;
person* newPerson = new person(nameIn, emailIn, iPhoneIn); //create node
您可以 fix/improve 您的代码有几种方式。
首先,我们将从明显错误的地方开始。当您从 cin 读取输入时,不要取消引用(调用 * 运算符)您的字符指针 (char*)。 cin 应该收到 char*,而不是 char。如果你给它传递一个 char,它只会写你输入的第一个字符。因此,如果要输入字符串,则需要将 char* 传递给它。
cin >> *nameIn; // this should be: cin >> nameIn;
cin >> *emailIn; // this should be cin >> emailIn;
运行 这个片段有和没有建议的变化看看我的意思:
#include <iostream>
using namespace std;
int main() {
cout << "Input a string: ";
char * c = new(char[200]);
cin >> *c; // only writes the first input character. Change to: cin >> c;
cout << "You input: " << c; // prints the first input character then garbage
delete[](c);
return 0;
}
其次,不要用整数表示 phone 数字。 int phone;
不适用于标准的 10 位 phone 号码。在大多数平台上,可以用整数表示的最大值约为 21 亿。大多数 10 位数字都大于 21 亿。请改用字符数组。相应地调整您的输入法。
第三,不要不必要地动态分配内存。如果您从一开始就知道所有名称都适合 32 个字符,那么就这样声明它们。
char *name; // change to char name[32];
char *email; // change to char email[32];
这会 运行 更快,并且让您免于必须释放已分配内存的痛苦。您将能够完全删除析构函数。