M
mcdougal.robert
Alright here is the issue. To begin with I am new to c++ and I am a
little lost of this homework assingment. The assignment was to allow
the user to enter in information about a small company and then have
that information sorted in descending order by years of service and
then written to a file. I have everything down except for the sort.
This is what I got can someone please help!!!!!!!!!!!
HEADER FILE employee.h
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#ifndef __EMP_H_
#define __EMP_H__
class employee
{
private:
int id;
char sex;
double wage;
int years;
public:
//Constructor
employee();
//Overloaded Constructor
employee( int uid, char usex, double uwage, int uyears );
//A get and set for Employee ID
void setid( int uid );
int getid( );
//A get and set for Employee Sex
void setsex( char usex );
char getsex( );
//A get and set for Employee Wage
void setwage( double uwage );
double getwage( );
//A get and set for Employee Years w/ Company
void setyears( int uyears );
int getyears( );
};
#endif
CLASS DEFINITION employee.cpp
#include <iostream>
#include "stdafx.h"
#include "employee.h"
#include <string>
using namespace std;
//Constructor
employee::employee() {
id = 0;
sex = 'x';
wage = 0.0;
years = 0;
};
//Overloaded Constructor
employee::employee( int uid, char usex, double uwage, int uyears ) {
id = uid;
sex = usex;
wage = uwage;
years = uyears;
};
//A get and set for Employee ID
void employee::setid( int uid ) {
id = uid;
};
int employee::getid( ) {return id;};
//A get and set for Employee Sex
void employee::setsex( char usex ) {
sex = usex;
};
char employee::getsex( ) {return sex;};
//A get and set for Employee Wage
void employee::setwage( double uwage ) {
wage = uwage;
};
double employee::getwage( ) {return wage;};
//A get and set for Employee Years w/ Company
void employee::setyears( int uyears ) {
years = uyears;
};
int employee::getyears( ) {return years;};
MAIN CODE
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <conio.h>
#include "employee.h"
#include <vector>
#include <cctype>
#include <fstream>
#include <algorithm>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int EMPLOYEES = 3;
string filename = "c:\\employee.dat";
int uid = 0;
char usex = 'q';
double uwage = 0.10;
int uyears = 100;
employee e;
vector<employee> etable;
ofstream outFile(filename.c_str());
if(outFile.fail())
{
cout << "\nFailed to open the data file." << endl;
exit(1);
}
outFile << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
for (int i = 0; i < EMPLOYEES; i++) {
cout << "\nRecord " << i + 1 << " of " << EMPLOYEES << endl;
cout << "\nEnter the Employees ID: ";
cin >> uid;
do {cout << "\nEnter the Employees Sex: ";
cin >> usex;
usex = toupper(usex);
if (usex != 'M' && usex != 'F') {
cout << "\nYou did not enter in a valid Sex Please enter in either
M/F.\n";
}
}
while(usex != 'M' && usex != 'F');
cout << "\nEnter the Employees Wage: ";
cin >> uwage;
cout << "\nEnter the Employees Years with the Company: ";
cin >> uyears;
cout << endl << endl << endl;
e = employee(uid, usex, uwage, uyears);
etable.push_back(e);
}
sort(etable.begin(), etable.end());
for(int i = 0; i < EMPLOYEES; i++)
{
outFile << etable.getid() << " "
<< etable.getsex() << " "
<< etable.getwage() << " "
<< etable.getyears() << endl;
}
_getch();
return 0;
}
little lost of this homework assingment. The assignment was to allow
the user to enter in information about a small company and then have
that information sorted in descending order by years of service and
then written to a file. I have everything down except for the sort.
This is what I got can someone please help!!!!!!!!!!!
HEADER FILE employee.h
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#ifndef __EMP_H_
#define __EMP_H__
class employee
{
private:
int id;
char sex;
double wage;
int years;
public:
//Constructor
employee();
//Overloaded Constructor
employee( int uid, char usex, double uwage, int uyears );
//A get and set for Employee ID
void setid( int uid );
int getid( );
//A get and set for Employee Sex
void setsex( char usex );
char getsex( );
//A get and set for Employee Wage
void setwage( double uwage );
double getwage( );
//A get and set for Employee Years w/ Company
void setyears( int uyears );
int getyears( );
};
#endif
CLASS DEFINITION employee.cpp
#include <iostream>
#include "stdafx.h"
#include "employee.h"
#include <string>
using namespace std;
//Constructor
employee::employee() {
id = 0;
sex = 'x';
wage = 0.0;
years = 0;
};
//Overloaded Constructor
employee::employee( int uid, char usex, double uwage, int uyears ) {
id = uid;
sex = usex;
wage = uwage;
years = uyears;
};
//A get and set for Employee ID
void employee::setid( int uid ) {
id = uid;
};
int employee::getid( ) {return id;};
//A get and set for Employee Sex
void employee::setsex( char usex ) {
sex = usex;
};
char employee::getsex( ) {return sex;};
//A get and set for Employee Wage
void employee::setwage( double uwage ) {
wage = uwage;
};
double employee::getwage( ) {return wage;};
//A get and set for Employee Years w/ Company
void employee::setyears( int uyears ) {
years = uyears;
};
int employee::getyears( ) {return years;};
MAIN CODE
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <conio.h>
#include "employee.h"
#include <vector>
#include <cctype>
#include <fstream>
#include <algorithm>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int EMPLOYEES = 3;
string filename = "c:\\employee.dat";
int uid = 0;
char usex = 'q';
double uwage = 0.10;
int uyears = 100;
employee e;
vector<employee> etable;
ofstream outFile(filename.c_str());
if(outFile.fail())
{
cout << "\nFailed to open the data file." << endl;
exit(1);
}
outFile << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
for (int i = 0; i < EMPLOYEES; i++) {
cout << "\nRecord " << i + 1 << " of " << EMPLOYEES << endl;
cout << "\nEnter the Employees ID: ";
cin >> uid;
do {cout << "\nEnter the Employees Sex: ";
cin >> usex;
usex = toupper(usex);
if (usex != 'M' && usex != 'F') {
cout << "\nYou did not enter in a valid Sex Please enter in either
M/F.\n";
}
}
while(usex != 'M' && usex != 'F');
cout << "\nEnter the Employees Wage: ";
cin >> uwage;
cout << "\nEnter the Employees Years with the Company: ";
cin >> uyears;
cout << endl << endl << endl;
e = employee(uid, usex, uwage, uyears);
etable.push_back(e);
}
sort(etable.begin(), etable.end());
for(int i = 0; i < EMPLOYEES; i++)
{
outFile << etable.getid() << " "
<< etable.getsex() << " "
<< etable.getwage() << " "
<< etable.getyears() << endl;
}
_getch();
return 0;
}