Ok, the updateName function doesn't hang anymore, but
it doesn't work either. It's supposed to replace
one record's data with new data. I think at least
part of the problem is the char data types and
their comparisons.
Still no luck getting printName to run just valid records, though. New code is:
Code:
void updateName(const string& filename)
{
fstream personUpdate(filename.c_str());
string last, first;
// char newfirst[15], newlast[15], newage[4];
person id;
cout << "Enter last and first name of account to update: ";
cin >> last >> first;
cout << "Enter new last and first name and age: ";
cin >> id.lastName >> id.firstName >> id.age;
// personUpdate.read((char *)&id, sizeof(person));
while (!personUpdate.eof())
{
personUpdate.read((char *)&id, sizeof(person));
if (strcmp(id.lastName, last.c_str()) == 0 && strcmp(id.firstName, first.c_str()) == 0)
{
// cout << "Enter new record (last name, first name, age): ";
cin >> id.lastName >> id.firstName >> id.age;
personUpdate.write((char*)&id, sizeof(person));
}
personUpdate.read((char *)&id, sizeof(person));
}
}
//void deleteName(const string& filename)
//{
//}
void printName(const string& filename)
{
ifstream personPrint(filename.c_str());
person blank;
int k = 1;
// for(int k = 0; k < 100; k++)
personPrint.read((char *)&blank, sizeof(person));
while (!personPrint.eof())
{
// if (blank.lastName != "unassigned")
// personPrint.seekg(k * sizeof(person));
// personPrint.read((char *)&blank, sizeof(person));
if (blank.age != "0 ")
cout << k << ": " << blank.lastName << " "<< blank.firstName << " "<< blank.age << endl;
personPrint.read((char *)&blank, sizeof(person));
k++;
}
}