Reputation: 243
I'm trying to write this program that keeps an employee database in a random access file, it has to have functions to add employees and remove employees (by writing all spaces in the record). This is what I have so far, but it doesn't work quite right. When reading an employee, it reads the salary of the correct record, but the name of the next record. Also, when I remove the last record and the add an employee in that record, I can't view the employee info, I get an exception error.
I'm not looking for the solution here, just a nudge in the right direction. Thanks.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include "ccc_empl.h"
using namespace std;
const int NEWLINE_LENGTH = 2;
const int RECORD_SIZE = 30 + 10 + NEWLINE_LENGTH;
/**
converts a string to a floating-point value
@param s a string representing a floating-point value
@return the equivalent floating-point value
*/
double string_to_double(string s)
{
istringstream instr(s);
double x;
instr >> x;
return x;
}
/*
reads an employee record from the input file
@param e the employee
@param in the file to read from
*/
Employee read_employee(istream& in)
{
string line;
getline(in, line);
string input_name = line.substr(0, 30);
double input_salary = string_to_double(line.substr(30, 10));
Employee e(input_name, input_salary);
return e;
}
/*
gets input for an Employee object
@param input_name the name of the employee
@param input_salary the salary of the employee
@param e the Employee object
@return returns the Employee object
*/
Employee input_employee()
{
string input_name;
cout << "Name: ";
cin.ignore();
getline(cin, input_name);
cout << "Salary: ";
double input_salary;
cin >> input_salary;
Employee e(input_name, input_salary);
return e;
}
/**
adds an employee record to a file
@param e the employee record to write
@param out the file to write to
*/
void add_employee(Employee e, ostream& out)
{
out << e.get_name()
<< setw(30)
<< fixed << setprecision(2)
<< e.get_salary();
}
/**
removes an employee record from a file
@param e the employee record to remove
@param out the file to remove from
*/
void remove_employee(ostream& out)
{
out << " " << setw(42) << fixed << setprecision(2) << " \n";
}
int main()
{
cout << "Please enter the data file name: ";
string filename;
cin >> filename;
fstream fs;
fs.open(filename);
fs.seekg(0, ios::end); // Go to end of file
int nrecord = fs.tellg() / RECORD_SIZE; // determine number of records in the file
int menu_input = 0;
string input_name;
double input_salary = 0;
while (menu_input != 4)
{
cout << "Please enter the record to update: (0 - " << nrecord - 1 << ") exit to quit ";
int pos = 0;
cin >> pos;
if(cin.fail())
{
cout << "Exiting..." << endl;
system("pause");
return 0;
}
// menu for user input
cout << "\nWhat action would you like to perform?" << endl;
cout << "Add employee.....1" << endl;
cout << "Remove employee..2" << endl;
cout << "View employee....3" << endl;
cin >> menu_input;
switch(menu_input)
{
case 1: fs.seekg(pos * RECORD_SIZE, ios::beg);
add_employee(input_employee(), fs);
break;
case 2: fs.seekg(pos * RECORD_SIZE, ios::beg);
remove_employee(fs);
break;
case 3: fs.seekg(pos * RECORD_SIZE, ios::beg);
cout << "\nName: " << read_employee(fs).get_name() << "Salary: " << read_employee(fs).get_salary() << endl << endl;
break;
default: cout << "Invalid entry" << endl;
break;
}
}
fs.close();
system("pause");
return 0;
}
Ok, here is the new and improved (well, I think so) code. The only problem I have is when adding an employee, I can get the program to add it to the first open record just fine, but if there are no open records, I can't seem to get it to add to the end of the file without messing up adding to the first empty record. What I mean is, if there is an empty record, it will add the employee to the record, but if there is no empty record, it doesn't add the employee to the end of the file. If I add code to add to the end of the file, one of two things happens, either it adds to the empty record, then asks for another employee and adds it to the end, or it just skips the empty record and adds to the end of the file.
Not sure what I'm doing wrong here, but any hints would be appreciated.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include "ccc_empl.h"
using namespace std;
const int NEWLINE_LENGTH = 2;
const int RECORD_SIZE = 30 + 10 + NEWLINE_LENGTH;
/**
converts a string to a floating-point value
@param s a string representing a floating-point value
@return the equivalent floating-point value
*/
double string_to_double(string s)
{
istringstream instr(s);
double x;
instr >> x;
return x;
}
/*
reads an employee record from the input file
@param e the employee
@param in the file to read from
*/
Employee read_employee(istream& in)
{
string line;
getline(in, line);
string input_name = line.substr(0, 30);
double input_salary = string_to_double(line.substr(30, 10));
Employee e(input_name, input_salary);
return e;
}
/*
gets input for an Employee object
@param input_name the name of the employee
@param input_salary the salary of the employee
@param e the Employee object
@return returns the Employee object
*/
Employee input_employee()
{
string input_name;
cout << "Name: ";
cin.ignore();
getline(cin, input_name);
cout << "Salary: ";
double input_salary;
cin >> input_salary;
Employee e(input_name, input_salary);
return e;
}
/**
adds an employee record to a file
@param e the employee record to write
@param out the file to write to
*/
void add_employee(Employee e, ostream& out)
{
out << e.get_name()
<< setw(10 + 30 - e.get_name().length())
<< fixed << setprecision(2)
<< e.get_salary() << "\n";
}
/**
removes an employee record from a file
@param e the employee record to remove
@param out the file to remove from
*/
void remove_employee(ostream& out)
{
out << " " << setw(40) << fixed << setprecision(2) << " \n";
}
int main()
{
cout << "Please enter the data file name: ";
string filename;
cin >> filename;
fstream fs;
fs.open(filename);
fs.seekg(0, ios::end); // Go to end of file
int nrecord = fs.tellg() / RECORD_SIZE; // determine number of records in the file
int menu_input = 1;
string input_name;
double input_salary = 0;
while (menu_input)
{
// menu for user input
cout << "\nWhat action would you like to perform?" << endl;
cout << "Add employee.....1" << endl;
cout << "Remove employee..2" << endl;
cout << "View employee....3" << endl;
cout << "Exit.............4" << endl;
cin >> menu_input;
if (menu_input == 4)
{
cout << "\nExiting..." << endl << endl;
system("pause");
return 0;
}
// switch statment to perform selected menu_input task
switch(menu_input)
{
case 1: // adds an employee in the first empty record
// or at the end of the file if no records are empty
{
int count = 0;
string s;
for (int i = 0; i < nrecord; i++)
{
fs.seekp(count, ios::beg);
getline(fs, s);
if (isspace(s[0]))
{
fs.seekp(count, ios::beg);
add_employee(input_employee(), fs);
nrecord++;
break;
}else
count += 42;
}
}
break;
case 2: {
cout << "Please enter the record to remove: (0 - " << nrecord -1 << ") ";
int pos = 0;
cin >> pos;
fs.seekp(pos * RECORD_SIZE, ios::beg);
remove_employee(fs);
}
break;
case 3: {
cout << "Please enter the record to view: (0 - " << nrecord -1 << ") ";
int pos = 0;
cin >> pos;
fs.seekg(pos * RECORD_SIZE, ios::beg);
Employee e(read_employee(fs));
cout << "\nName: " << e.get_name() << "Salary: " << e.get_salary() << endl << endl;
}
break;
default: cout << "Invalid entry" << endl;
break;
}
}
fs.close();
system("pause");
return 0;
}
Upvotes: 4
Views: 1077
Reputation: 437
It seems you never add the line terminator in add_employee, and read_employee is called twice. Just cache the result of the function.
I know this is just homework, but I think you can improve this program.
Need the database stay as a file at runtime? You could just read the entire file into an array of Employee and rewrite it at the end. It wouldn't limit the length of the employee names.
This program is not cross-platform. You shouldn't rely on file size to determine the number of entries in the database, since line terminators vary from os to os. Instead, read it all, and count the entries.
Only windows has the 'pause' program. To achieve the same effect, just read stdin and at the end of the program and ignore the input.
Upvotes: 0
Reputation: 15278
You call read_employee(fs)
twice. First reads record you want, the second reads the next one (standard doesn't specify which of them is 'first').
Upvotes: 1