problem with inputing all the values to file

K

Kelly

EmployeeTable<Employee> employees;//where EmployeeTable is the array
that stores the employees
PositionIndex<EmployeeString>employee_nameIndex;//index for the
array by last name and first name
for( int i = 0; i < 3; i++ )
{
cout << "Enter Employee info:" << endl;
Employee employee;
cin >> employee;
employees.push_back( employee );
Pair<EmployeeString, unsigned long> nameIndexEntry(
employee.getNameKey(), i );
employee_nameIndex.push_back( nameIndexEntry );
}


// sort index
employee_nameIndex.sort();

// search for employee by name
char s = 0;
EmployeeString firstName;
EmployeeString lastName;
EmployeeString ssn;


cout << "Search for Employee: " << endl;
do
{
cout << "First name:";
cin >> firstName;
cout << "\nLast name:";
cin >> lastName;
cout << "\nSSN:";
cin >> ssn;
long pos = employee_nameIndex.search( lastName + firstName +ssn
);
if ( pos < 0 )
{
cout << "Employee NOT found" << endl;
}
else
{
cout << "employee found: " << endl;
cout << employees[pos];
}
cout << "Search another employee(y/n)?" << endl;
s = getch();
}while( s != 'n' );


ofstream of;
cout << "Save following Employee to Employees.dat" << endl;
cout << employees;
of.open( "employees.dat", ios_base::binary );
of << employees;
of.close();


EmployeeTable<Employee> employeesIn;
ifstream inf;
inf.open( "employees.dat", ios_base::binary );
inf >> employeesIn;
inf.close();
inf.seekg(0);


cout << "Following is Employees from employees.dat" << endl;
cout << employeesIn;


}

My array is overwritting on previous records, every time i enter values
into the record.
Please help
Kelly
 
V

Victor Bazarov

Kelly said:
[...]
My array is overwritting on previous records, every time i enter values
into the record.
Please help

This is FAQ 5.8.

My old teacher of theoretical mechanics used to say that a good drawing,
scheme, picture, illustration, gives 50% of a solution to the problem.
The code you posted (I couldn't even force myself to keep it quoted) is
so badly organized (whitespace-wise, indentation-wise, comments), nobody,
including yourself, can find the ends of the issue in it without first
reformatting and reindenting the whole thing. Besides, it's incomplete.

Take the time to read the FAQ and follow its instructions.

V
 
H

Howard

Victor Bazarov said:
The code you posted (I couldn't even force myself to keep it quoted) is
so badly organized (whitespace-wise, indentation-wise, comments), nobody,
including yourself, can find the ends of the issue in it without first
reformatting and reindenting the whole thing. Besides, it's incomplete.

V

Hi Victor,

you know, it may be that the code is indented fine in his editor. On my
PC, if I indent using tabs, my Outlook Express software removes them when I
post to here. I have to explicitly use spaces in my code in order to get
indentation to show up. So what I'm saying is, maybe we shouldn't jump on
everyone who posts code that looks like it's not indented...it could just be
their newsreader that screws it up!

-Howard
 
V

Victor Bazarov

Howard said:
Hi Victor,

you know, it may be that the code is indented fine in his editor. On my
PC, if I indent using tabs, my Outlook Express software removes them when I
post to here. I have to explicitly use spaces in my code in order to get
indentation to show up. So what I'm saying is, maybe we shouldn't jump on
everyone who posts code that looks like it's not indented...it could just be
their newsreader that screws it up!

Hi Howard,

You know, it may be that the OP is using such a bad newsreader that they
won't be able to ever post the code well-indented. I can live with that.
But when it's not just not indented but also incomplete and not even
described well, you will have to excuse me. So what I'm saying is, maybe
you will not jump on _me_ for trying to educate newbies how to post in
comp.lang.c++...
 
J

Jesper Madsen

If your example example is the sequence of your code, then you need to read
from the file first, let the "user" enter employees, and then save to a
file.

In your sample you let the user enter some data, then saves to file, and
then read from the file. Your data will be overwritten...

Jesper
 
H

Howard

Victor Bazarov said:
Hi Howard,

You know, it may be that the OP is using such a bad newsreader that they
won't be able to ever post the code well-indented. I can live with that.
But when it's not just not indented but also incomplete and not even
described well, you will have to excuse me. So what I'm saying is, maybe
you will not jump on _me_ for trying to educate newbies how to post in
comp.lang.c++...

Sorry, Victor, didn't mean to jump on you. It's just that every time
someone posts unindented code, they get jumped on, and I wanted to suggest
easing up on that particular subject a little. (By the way, I did like your
use of my phrasing to reply back to me...very creative! :))

-H
 
D

David Harmon

On 22 Dec 2004 13:07:10 -0800 in comp.lang.c++, "Kelly"
My array is overwritting on previous records, every time i enter values
into the record.

Cannot tell from your code if you are even reading employee record
successfully, or doing so more than once. Consider something more
like

cout << "Enter Employee info:" << '\n'; // eschew endl
Employee employee;
while (cin >> employee) {
cout << i++ << ": " << employee << '\n';
// do stuff with it...

This issue is covered in Marshall Cline's C++ FAQ. See section 15,
especially the topic "[15.6] Why is my program ignoring my input
request after the first iteration?" It is always good to check the
FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
J

Jonathan Mcdougall

Howard said:
you know, it may be that the code is indented fine in his editor. On my
PC, if I indent using tabs, my Outlook Express software removes them when I
post to here.

Either don't use Outlook Express or use spaces. Most editors offer an
option to convert some text from tabs to spaces.
I have to explicitly use spaces in my code in order to get
indentation to show up.

Is that so bad for some free help?
So what I'm saying is, maybe we shouldn't jump on
everyone who posts code that looks like it's not indented...it could just be
their newsreader that screws it up!

Maybe people should learn to use their newsreader first.


Jonathan
 
K

Karl Heinz Buchegger

Howard said:
[snip]
you know, it may be that the code is indented fine in his editor. On my
PC, if I indent using tabs, my Outlook Express software removes them when I
post to here. I have to explicitly use spaces in my code

.... which is a good idea anyway, since there is no standard on the stop
points of one tab. 8 is common but can usually in todays editors be reset
to anything you want. If you ever get a source code from someone whoes tab
definition does not match yours, you will execrate the inventor of tab.
 
H

Howard

Jonathan Mcdougall said:
Either don't use Outlook Express or use spaces. Most editors offer an
option to convert some text from tabs to spaces.

I DO use spaces, as I said below! (I have had jobs, however, where the use
of tabs in code was required. And it's hardly your place to tell me what
software to use. OE is the most common newsreader in the world, I'd bet.)
Is that so bad for some free help?

Who said it's bad? I do it all the time now. I simply suggested that the
REASON that many posts come with no indentation MAY be that they use a
newsreader that stripped their tabs.
Maybe people should learn to use their newsreader first.

What a lovely, generous attitude. All I'm suggesting is a little tolerance
for posters whose code shows up unindented. Maybe a polite "you might want
to use spaces instead of tabs, because this is unreadable", or something
similar. Is tolerance so hard, or so evil?

Merry Christmas!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top