Problem with sorting a vector

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mcdougal.robert@gmail.com

    Problem with sorting a vector

    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::emplo yee() {
    id = 0;
    sex = 'x';
    wage = 0.0;
    years = 0;
    };

    //Overloaded Constructor
    employee::emplo yee( 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::setse x( char usex ) {
    sex = usex;
    };
    char employee::getse x( ) {return sex;};

    //A get and set for Employee Wage
    void employee::setwa ge( double uwage ) {
    wage = uwage;
    };
    double employee::getwa ge( ) {return wage;};

    //A get and set for Employee Years w/ Company
    void employee::setye ars( int uyears ) {
    years = uyears;
    };
    int employee::getye ars( ) {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.d at";
    int uid = 0;
    char usex = 'q';
    double uwage = 0.10;
    int uyears = 100;
    employee e;
    vector<employee etable;

    ofstream outFile(filenam e.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_bac k(e);


    }

    sort(etable.beg in(), etable.end());

    for(int i = 0; i < EMPLOYEES; i++)
    {
    outFile << etable[i].getid() << " "
    << etable[i].getsex() << " "
    << etable[i].getwage() << " "
    << etable[i].getyears() << endl;
    }


    _getch();




    return 0;
    }

  • Peyman

    #2
    Re: Problem with sorting a vector



    On Oct 31, 9:57 am, mcdougal.rob... @gmail.com wrote:
    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.beg in(), 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.beg in(), 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.

    Comment

    • Peyman

      #3
      Re: Problem with sorting a vector

      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::predi cate( // the rest are same!

      or make as friend if you know how!

      and change the sort function call to this:

      sort(etable.beg in(), etable.end(), employee::predi cate);

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

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

      and then simply say:
      sort(etable.beg in(), 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!

      Comment

      • mcdougal.robert@gmail.com

        #4
        Re: Problem with sorting a vector

        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.

        Comment

        • Peyman

          #5
          Re: Problem with sorting a vector



          On Oct 31, 10:38 am, mcdougal.rob... @gmail.com wrote:
          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.

          Comment

          • red floyd

            #6
            Re: Problem with sorting a vector

            mcdougal.robert @gmail.com 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 ]

            Comment

            • Eric

              #7
              Re: Problem with sorting a vector

              red floyd wrote:
              2. Never put "using namespace std;" into a header file.
              Why?

              Comment

              • Andre Kostur

                #8
                Re: Problem with sorting a vector

                Eric <no@thanks.comw rote in news:ei7i6v$pr$ 1@geraldo.cc.ut exas.edu:
                red floyd wrote:
                >2. Never put "using namespace std;" into a header file.
                >
                Why?
                >
                Because then you're inflicting your decision onto anybody who includes your
                header file.

                Comment

                • BlackJackal

                  #9
                  Re: Problem with sorting a vector

                  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?

                  Comment

                  • BobR

                    #10
                    Re: Problem with sorting a vector


                    BlackJackal wrote in message
                    <1162508652.216 128.211980@e3g2 000cwe.googlegr oups.com>...
                    >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::ostringstr eam 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.";
                    }

                    --
                    Bob R
                    POVrookie


                    Comment

                    Working...