-
What's wrong here?
The printName function here is supposed to print just the records with data, but instead prints all 100 records. Records are a struct char lastName[15], firstname[15], age[15]. Empty records are initialized to "unassigned', "", "0", respectively. Some comments represent ways I've tried to get it to work, but no luck. What's going wrong here?
Also, function update hangs after: cout << "Enter last and first name of account to update: ". That I'm still working on, but any clues there?
Thanks again.
Code:
void update(const string& filename)
{
fstream personUpdate(filename.c_str());
string last, first;
person id;
cout << "Enter last and first name of account to update: ";
cin >> last >> first;
personUpdate.read((char *)&id, sizeof(person));
while (!personUpdate.eof())
{
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));
}
}
}
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++;
}
}
-
cin
I odn't think you can
cin << x << y;
have you tried seperate them?
cycbersnaek
PS: please put comments on the program. So we know what you trying to do without going thru the code.
-
You cin"ed" the wrong way!
Should be: cin>>x>>y;
cin>>x;
cin>>y;
---------
Engineer223
alias:322reenignE
-
checked
just wrote a fast program, yep >> :)
you can cin >>x>>y>>z; too.
my bad
Cycbersnaek
-
Well, cin >> last >> first; should work, but I tried it the other way just in case and it still didn't work. That's when update hangs.
Why does printName print all 100 records, instead of just the 3 or 4 I enter with valid data? And why does update hang?
-
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++;
}
}
-
repeat this line:
personUpdate.read((char *)&id, sizeof(person));
here /////////////////////////////////////////////// in the following code
personUpdate.read((char *)&id, sizeof(person));
while (!personUpdate.eof())
{
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));
}
///////////////////////////////
}
otherwise you will never get to the end of file (unless the file was empy or there was only the one record in the file) and I think you will be in an endless loop.
The following while loop will not stop until you find EOF as there is no limit to k.
int k = 1; //I added this line to make the loop make sense
personPrint.read((char *)&blank, sizeof(person));
while (!personPrint.eof())
{
if (blank.age != "0 ")
cout << k << ": " << blank.lastName << " "<< blank.firstName << " "<< blank.age << endl;
personPrint.read((char *)&blank, sizeof(person));
k++;
}
To read just the first three records change the while statement to
while (!personPrint.eof() && k < 4)
-
If I'm reading you correctly, I did add that code to update in the post before yours. update doesn't hang now, but doesn't overwrite the old record with new data either. It doesn't do anything, as far as I can tell.
I could print out just the valid records if the number of records was known, like you wrote, but (in theory) the number may not be known, and all valid records may not be contiguous in any case. The program needs to loop through all records and just print those with data. I've tried stuff like if (id.lastName != "unassigned"), if (id.age != "0"), to test for empty records (those values are what empty records are initialized to), but it doesn't work. Compiles and runs, just doesn't work correctly.
It may be a similar problem in both functions, because it seems the test isn't catching the field values like it should. This is where I don't know what the problem is.
Thanks for your help.