我对C ++很陌生,而且我正在进行这个项目,我一直试图将我的大脑搁置一段时间。
基本上,我想在这个"数据库中拥有10条用户记录。"现在我有薪水和year_hired分别为double和int。我想最终将它们变成一个数组。
我在这里遇到了几个问题:
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();
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
创建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;
}
}
我希望能够将struct下的year_hired转换为一个字符串,该字符串最终使用字符串存储hire_date。我还想将薪水更改为一个也可以显示的字符串。
但是,现在,我有一个问题从帐户输出名称。
你们有关于如何解决字符数组这个问题的任何提示吗?我提供了迄今为止的代码。提前谢谢!
编辑:我能够通过使用下面的答案解决数组的问题
if (n.size() >= sizeof name) {
cout << "Name " << n << " is too long\n";
}
else {
strcpy_s(name, n.c_str());
}
我仍在努力解决更新新记录和保存更改的问题。感谢您的回复!
编辑2:嘿!我想出了更新帐户的问题。这是上面的问题#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));
答案 0 :(得分:0)
您无法使用指针复制C字符串,您需要使用strcpy()
。
void setname(string n) {
if (n.size() >= sizeof name) {
cout << "Name " << n << " is too long\n";
} else {
strcpy(name, n.c_str());
}
}
此外,
ptr = &name[30];
是指向name
数组末尾之外的指针。数组的最后一个元素是name[29]
。