检索字符数组数据时出现问题

issues retrieving character array data

本文关键字:问题 数据 字符 数组 检索      更新时间:2023-10-16

我对C++还很陌生,我正在进行这个项目,我已经试着绞尽脑汁了一段时间。

基本上,我想在这个"数据库"中有10条用户记录。现在我的工资和年雇佣数分别为double和int。我想最终把它们变成一个数组。

我在这里面临几个问题:

  1. 当函数outputLine调用要从一名员工处查看的数据时,除了"名称部分"(它会出现胡言乱语)之外,一切都显示得很好。请参见以下内容:

Enter employee ID (1-10) or 0 to end input:
5
Enter name, salary, then year hired:
test
130000
2009
Enter employee ID (1-10) or 0 to end input:
0
Enter choice 2 to choose employees:
2
Enter your choice:
1 - update an account
2 - add a new account
3 - end program
1
Enter account to update(1-10): 5
5
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
130000
2009

显示名称的代码如下。我还包含了我的洞察结构:

struct employee {
int year_hired;
double salary;
char name[30];
int ID;
void outputLine(ostream &output, employee &employed)
{
output << employed.getId() << endl
<< employed.getName() << endl
<< employed.getsal() << endl
<< employed.getYearHired() << endl;
}
void setname(string n) {
const char *ptr = &name[30];
ptr = n.c_str();
  1. 当我尝试使用程序的功能"更新"帐户时,我输入的工资不会更改存储的工资。请参阅下面的复制粘贴数据以供参考:

Enter account to update(1-10): 5
5
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
130000
2009
Enter salary: 20
Enter your choice:
1 - update an account
2 - add a new account
3 - end program
1
Enter account to update(1-10): 5
5
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
130000
2009

  1. 当提示用户"添加"新帐户时,它会检查该帐户是否有信息。即使我还没有设置该帐户,程序也会告诉我该帐户存储了信息。我的代码中有一部分专门用于构建10个空白记录以供输入,但它似乎并没有真正起到任何作用

创建10条空白记录的部分:

ofstream outdatabase("database.dat", ios::out | 
ios::binary);
for (int i = 0; i < 10; i++) {
outdatabase.write(reinterpret_cast <const char *> 
(&blankelist), sizeof(employee));
}

添加新记录的部分:

void newrecord(fstream &insertinfile) {
int id = getid("Enter new employee ID");
insertinfile.seekg((id - 1) * sizeof(employee));
employee employed;
insertinfile.read(reinterpret_cast <char *>(&employed), sizeof(employee));
if (employed.getId() == 0)
{
string name;
double salary;
int year_hired;
cout << "Enter name, balance, and year hired: " << endl;
cin >> name;
cin >> salary;
cin >> year_hired;
employed.setname(name);
employed.setSalary(salary);
employed.setYearHired(year_hired);
employed.setId(id);
insertinfile.seekp((id- 1) * sizeof(employee));
insertinfile.write(reinterpret_cast<const char *> (&employed), 
sizeof(employee));
}
else {
cerr << "Employee ID: " << id
<< " already contains information." << endl;
}
}

我希望能够将year_hored under结构转换为一个字符串,该字符串最终用字符串存储hire_date。我还想把工资改成一个字符串,也可以显示出来。

然而,现在,我在从帐户输出名称时遇到问题。

关于如何解决字符数组的问题,你们有什么建议吗?我已经提供了迄今为止的代码。提前感谢!

编辑:我能够通过使用下面的答案来解决阵列的问题

if (n.size() >= sizeof name) {
cout << "Name " << n << " is too longn";
}
else {
strcpy_s(name, n.c_str());
}

我仍在通过更新新记录和保存更改来修复这个问题。感谢您到目前为止的回复!

第二版:嘿!我发现了更新帐户的问题。

这是上面的问题2。我的代码是

void updaterecord(fstream &updateFile) {
int id = getid("Enter account to update");
{
updateFile.seekg((id - 1) * sizeof(employee));
employee employed;
updateFile.read(reinterpret_cast <char *> (&employed), sizeof(employee));
if (employed.getId() != 0)
{
outputLine(cout, employed);
cout << "nEnter the new name, salary, and year hired: ";
double salary;
string name;
int year_hired;
cin >> name >> salary >> year_hired;
double oldsalary = employed.getsal();
employed.setname(name);
employed.setSalary(salary);
employed.setYearHired(year_hired);
updateFile.seekp((id) * sizeof(employed));
}
else
{
cerr << "Account # " << id << " has no information." << endl;
}
}
}

修复:我更改并在函数末尾添加了以下内容:

updateFile.seekp((id - 1) * sizeof(employee));
updateFile.write(reinterpret_cast <const char*> (&employed), 
sizeof(employee));

不能使用指针赋值来复制C字符串,需要使用strcpy()

void setname(string n) {
if (n.size() >= sizeof name) {
cout << "Name " << n << " is too longn";
} else {
strcpy(name, n.c_str());
}
}

此外,

ptr = &name[30];

是指向CCD_ 2数组末尾之外的指针。数组的最后一个元素是name[29]