Problem with sorting a vector

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;
}
 
P

Peyman

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!!!!!!!!!!!

some more here...

#include <conio.h>

Do not include this header file, this is not standard C++
some more here...

sort(etable.begin(), etable.end());

i suppose u need help 4 this part!

add this function to your program:

bool predicate(const employee& X, const employee& Y)
{ return (X.years < Y.years); }

and then call the sort function like this:

sort(etable.begin(), etable.end(), predicate);

the function i've written here is called a predicate function, which
helps you to tell the sort function, what does it mean for an employee
to be smaller than another employee! for the third parameter of the
sort function you can either use a function pointer or an object
function! if interested, find out what they are.
 
P

Peyman

CORRECTION:
as the member variable "year" is private, you cannot use it outside the
scope of class, so move the predicate function into your class, like
this:

static bool employee::predicate( // the rest are same!

or make as friend if you know how!

and change the sort function call to this:

sort(etable.begin(), etable.end(), employee::predicate);

alternative way, if you know how to overload operators, or even if not,
add this method to your class:

bool employee::eek:perator<(const employee& X, const employee& Y)
{return (X.years < Y.years);}

and then simply say:
sort(etable.begin(), etable.end());

now the sort function knows what does it mean for an emplyee to be less
than another one! and it's enough for sorting!
 
M

mcdougal.robert

Thank you so much for your help!!!! I am learning how to program via
distance learning so the only learning tool I have is the book and I
couldn't find where the book covered this part so without you I would
have been dead in the water.

Thank you for not only telling me what I needed but explaining its
purpose. I truly appreciate it.
 
P

Peyman

Thank you so much for your help!!!! I am learning how to program via
distance learning so the only learning tool I have is the book and I
couldn't find where the book covered this part so without you I would
have been dead in the water.

Thank you for not only telling me what I needed but explaining its
purpose. I truly appreciate it.

You're most welcome.
 
R

red floyd

(e-mail address removed) wrote:

Other people have dealt with your sort issues, so I'm going to deal with
two structural issues.
HEADER FILE employee.h

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
#ifndef __EMP_H_
#define __EMP_H__

1. __EMP_H__ is reserved to the implementation. Any identifier with a
double underscore is so reserved. You may not declare any such
identifiers in your code.

2. Never put "using namespace std;" into a header file.
> [ remainder of code redacted ]
 
B

BlackJackal

1. __EMP_H__ is reserved to the implementation. Any identifier with a
double underscore is so reserved. You may not declare any such
identifiers in your code.

2. Never put "using namespace std;" into a header file.
[ remainder of code redacted ]

First off, I appreciate the help. Secondly please excuse my ignorance
but what do you mean by reserved to the implementation? I am new to
programming so I may need a little more help. Also why would everyone
be bound to my decision by leaving the namespace in the header file?
 
B

BobR

BlackJackal wrote in message
1. __EMP_H__ is reserved to the implementation. Any identifier with a
double underscore is so reserved. You may not declare any such
identifiers in your code.

2. Never put "using namespace std;" into a header file.
[ remainder of code redacted ]

First off, I appreciate the help. Secondly please excuse my ignorance
but what do you mean by reserved to the implementation?

The people who write the compilers and libraries need to use variables, etc..
They need insurance that no end-user will stomp on the values, etc.. So, they
reserved a special way of nameing them. If you look in the headers that came
with your compiler, you'll see lots of names that start with leading
underscores.
I am new to programming so I may need a little more help.

No problem. We all need help at some time..."No man is an island!".
Also why would everyone
be bound to my decision by leaving the namespace in the header file?

Because anybody who included a header file with 'using namespace std;' would
have the whole std namespace opened up, and that may cause trouble.
Example:
In my Testbench program, I use an stringstream named 'cout' (saves having to
change a lot of code I copied from an NG post.).

std::eek:stringstream cout;

cout<< "This goes to a stream, output to a text window.";
std::cout<< "This is only seen by the IDE debugger (non-console app).";

Now if I included your header with the 'using ....' in it, my 'cout' does not
work ( and cause a compile error, I hope.). I would get mad real quick!!
[ Yes, 'cout' is a bad name to use, but, in my situation it works well. ]

Nobody says you can't do it, just that it is bad practice to put a 'using'
directive in a header file.

int main(){ // or: void MyFunction(int num){}
using std::cout; // less of a problem
cout<< "Hello World.";
}
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top