Help with C++ program

ngvepforever2

Golden Member
Oct 19, 2003
1,269
0
0
Hi I have been working in this program( C++). The program is a system that
stores an employee's firstname , lastname, five digit number, the
department they work in and their annual salary. There are a maximum
of teen employees in the organization.

The program should allow me to add an employee , but it should warn me
if I am trying to create two identical employees, it should be able to
delete a current employee or update an existing employee's
information. The data is saved in the file "employee2.txt" when shutdown
and retrieves the data from the same file when started. The format of
the file is

11111 Firstname Lastname Departmentname $10,000

Now up to this point I am able to add a new employee, but where the
problem comes is when trying to delete, update or compare an employee.
I think I should use a loop to search thru the file in order to get
the name of the employee to delete, or to update, if this works , the
loop should also allow me to compare two employees. But I am confused
becasue I don't know if I should use the get function in order to do
that. I cannot use arrays pointers or strings because in my class I haven't gotten to that point :(. Right now I am using simple classes to do this problem

This is what I got so far
#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

class INFO
{
private:

int memberid;
char firstname[10];
char lastname[10];
char departname[15];
char annualsalary[10];

public:

void addemployee(ofstream& out_stream, INFO employee);

void update(ofstream& out_stream, ifstream& in_stream, INFO
employee);
};

int main()
{
ifstream in_stream;
in_stream.open("employment2.txt");
ofstream out_stream;
out_stream.open("employment2.txt",ios::app);
INFO employee;
char choice;
cout<<"Enter your choice of preference.\n";
cin>>choice;

switch(choice)
{

case'A':

employee.addemployee(out_stream, employee);

break;

case 'B':

employee.update(out_stream, in_stream, employee);

break;


}

out_stream.close();
return 0;
}

//Uses iostream
//Uses fstream
//Uses uses iomanip

void INFO::addemployee(ofstream& out_stream, INFO employee)
{

cout<<"Please enter a new employee to be written in the file.\n";
cout<<"in this format 99999 firstname lastname departmentname
$10,000.\n";
cin>>employee.memberid;
cin>>employee.firstname;
cin>>employee.lastname;
cin>>employee.departname;
cin>>employee.annualsalary;

cout<<employee.memberid<<setw(10)<<employee.firstname<<setw(10)<<employee.lastname<<setw(15)<<employee.departname<<setw(10)<<employee.annualsalary<<endl;
out_stream<<employee.memberid<<setw(10)<<employee.firstname<<setw(10)<<employee.lastname<<setw(15)<<employee.departname<<setw(10)<<employee.annualsalary<<endl;

}


//Uses iostream
//Uses fstream
//Uses iomanip

void INFO::update(ofstream& out_stream, ifstream& in_stream, INFO
employee)

{//This is where I have the problem!
}



I need the function definitions to compare that I am not creating two similar employees and the function to update and delete a current employee, I also need to include constructors to initiliaze my variable employee
Regards and thanks again...this is my second post in Anandtech.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Now up to this point I am able to add a new employee, but where the
problem comes is when trying to delete, update or compare an employee.
I think I should use a loop to search thru the file in order to get
the name of the employee to delete, or to update, if this works , the
loop should also allow me to compare two employees.


Are you allowed to use ARRAYS? For instance, declare a array of 10 INFO's:

INFO records[10];
 

Spydermag68

Platinum Member
Apr 5, 2002
2,616
99
91
Have you your tried to use the string class?


Sorry I did not read the whole message about not using strings.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
The post is a little overwhelming, it's not clear to me exactly what you're asking. Generally you will get better answers when you ask very specific questions.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: ngvepforever2
I am not allowed to use arrays, but thanks for replying to my post :)


Your professor is an ass making you do this project without teaching you about arrays. You should also have been taught to use standard strings instead of character-string arrays.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Without using arrays, you only have a couple of options on how to do this project. I suggest that you use a temporary file to save the updated contents of your 'records' (for deleting/updating) and then write the data back to your original file.
 

ngvepforever2

Golden Member
Oct 19, 2003
1,269
0
0
Originally posted by: singh
Originally posted by: ngvepforever2
I am not allowed to use arrays, but thanks for replying to my post :)


Your professor is an ass making you do this project without teaching you about arrays. You should also have been taught to use standard strings instead of character-string arrays.


I know , my professor is an ass, and the stupid book I have does not help either, arrays is in chapter 10 and strings in chapter 12, on the other hand classes and structures is in chapter 6 , Thanks Walter Savitch and his "Problem Solving with C++", well I have posted this question in google and I will pay 30 bucks to whoever can help me to figure it out, it's killing me!, this is the link
http://answers.google.com/answers/threadview?id=273023

I have been thinking and I have come to the conclusiong that the only thing I can use in order to read a charachter from a function is the get() function, I figure I use a loop but I need help in doing this, how can I set it up?

thanks again
 

ngvepforever2

Golden Member
Oct 19, 2003
1,269
0
0
And if someone wants to look at the problem itself here it is :

I am the owner of a small business and am in need of a simple system to keep track of information about my employees. The system needs to store the employee?s name(first, last) their employee number ( a 5 digit number) the department they work in and the annual salary. The system should also be able to compare two employees and warn me if I am trying to create two identical employees. Currently there are a maximum of ten employees in the organization.

I would like for you to provide a text based user interface to allow me to add a new employee to the system, delte a current employee, or update an existing employee?s information. I am on a tight budget, so please minimize the cost of the system by reusing any parts from existing systems wherever possible. I would like to call the system EmployeeTracker and would like it to save the data in a file (employee.dat) when shutdowm and retrieve the data from the file when started. The format of the file should be as follows, one employee per line.


99999 Firstname Lastname Departmentname $10,000


 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: ngvepforever2
Originally posted by: BingBongWongFooey
What if there's a space in their first or last name?

I haven't thought of that, I think I should assume that's not going to occur :)

Bah, I'd be frustrated with such a contrived problem. For example, in real life, I'd tell the person that storing commas and dollar signs in the file is pointless and only makes it more annoying to code and possibly more error-prone.
 

MrMilney

Senior member
Aug 12, 2000
678
0
0
I'll give you some tips just because your prof isn't allowing you to use arrays (as asinine as that is). The easiest way I see to do it is:
  1. Modify your INFO class ctor to initialize memberid to a sentinel value (I suggest -1).
  2. Create 10 instances of your INFO class in main.
  3. When adding a new employee, loop through each of the 10 instanced of INFO until you find one with memberid -1. At that point you know you've found an empty INFO that you can record into. Be sure to look for duplicate memberid's on the way so that you don't enter the same employee twice.
  4. You'll have to do something similar when searching for, modifying or deleting existing records.
  5. Kick your prof in the shin for writing such a lame coding assignment. This is the kind of thing that'll drive you out of CS and into the arms of another major.
 

ngvepforever2

Golden Member
Oct 19, 2003
1,269
0
0
thanks man!, I got the tips that you gave me, hopefully I will get the program done now...and It's true what you said about changing majors , about half my class dropped out already but I am sticking there just because I don't like to give up.

Regards and thanks again

ngvepforever2
 

JDub02

Diamond Member
Sep 27, 2002
6,209
1
0
modifying a line in a text file was always a PITA to me.

if you can, read the entire file into memory, modify it there, and rewrite the entire file again.

if you can use a character array, then you know enough to use an array of INFO objects.


#define MAX 50

INFO EmployeeInfo[MAX];
int NumberEmployees = 0;

//read in file to EmployeeInfo array, keep track of number of employees

//option to add, delete, or modify

//if add, use NumberEmployees to add entry to the end

//if delete, search for employee number and remove from array ... bump the rest up to fill in the hole

//if modify, search for employee number and change data

//rewrite (overwrite) the file with the updated data.



my $.02 .... at least that's what I'd do without using pointers.