Here's my code:

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>

using namespace std;

class employee
{
   /* Employee class to contain employee data
   */
   
   private:
      string surname;
      double hourlyRate;
      int empNumber;
     
   public:
      employee() {
         hourlyRate = -1;
         empNumber = -1;
         surname = "";
      }
      
      employee(const employee &other) :
         surname(other.surname),
         hourlyRate(other.hourlyRate),
         empNumber(other.empNumber)
      {
         // copy constructor
      }
      
      void setEmployee(const string &name, double rate, int num) { 
         surname = name;
         hourlyRate = rate;
         empNumber = num;
      }
      
      const string& getSurname() const {
         return surname;
      }
      
      void printEmployees() {
         cout << fixed << setprecision(2);
         cout << setw(20) << left << surname << setw(4) << empNumber << "  " << hourlyRate << "\n";
      }
      
      void loadEmployee(ifstream &fin) {
           fin >> surname;
           fin >> hourlyRate;
           fin >> empNumber;
      }
      
};
      
void swap(vector<employee> employees, int a, int b);
void sortEmployees(vector<employee> &employees);
void printEmployees(vector<employee> &employees);
void loadEmployees(vector<employee> &employees, const char *file);

int main(int argc, char *argv[])
{
   vector<employee> employees;

   if (argc != 2) {
      cout << "Syntax : employee employeefile\n";
      return 0;
   }

   loadEmployees(employees, argv[1]);
   printEmployees(employees);
   sortEmployees(employees);
   printEmployees(employees);

   return 0;
}

void loadEmployees(vector<employee> &employees, const char *file)
{
   ifstream fin;
   employee emp;
   
   fin.open(file);
   if (!fin) {
      cout << "Unable to read from " << file << "\n";
      exit(0);
   }
   
   while (!fin.eof()) {
         emp.loadEmployee(fin);
         employees.push_back(emp);
   }
   fin.close();   
}

void printEmployees(vector<employee> &employees)
{
   unsigned int i;

   for (i=0; i<employees.size(); i++) {
      employees[i].printEmployees();
   }
   printf("\n");
}

void swap(vector<employee> employees, int a, int b)
{
   employee temp(employees[a]);

   employees[a] = employees[b];
   employees[b] = temp;
}

void sortEmployees(vector<employee> &employees)
{
   /* use selection sort to order employees, 
      in employee name order
   */

   int number = employees.size();
   int inner, outer, max;

   for (outer=number-1; outer>0; outer--)
   {
      // run though array number of times
      max = 0;
      for (inner=1; inner<=outer; inner++)
      {
         // find alphabeticaly largest surname
         if (employees[inner].getSurname() > employees[max].getSurname())
            max = inner;
      }
      if (max != outer)
      {
         // swap largest with last element in array
         swap(employees, max, outer);
      }
   }
}

See attachment... Basically that attachment is just showing the result of my code and the txt file that I used to run the code.. Above is my result and bellow is the expected result from my teacher... I want to change my result like the expected result.. I have no idea what should I do (noob alert)... Can anyone help me with this?

>> I have no idea what should I do (noob alert)...

A quick inspection of the output shows three things.

  1. Pre-sort results match the expected results.
  2. Post-sort results do not.
  3. Pre-sort and post-sort results are identical.

Therefore...your sort doesn't work. Two possibilities.

  1. Sort doesn't work inside the function.
  2. Sort works inside the function, but somehow the sort gets "lost" after the function.

Figure out which one it is. Print it out at the top of the function. Print it out at the bottom of the function. See which, if either, printout is correct. If both are correct, then it's a parameter passing issue. If the printout at the top of the function is right and the printout at the bottom isn't, take a close look at the function itself.

So, how/where does your result generate incorrect results (other than that they differ from what your teacher provided)? We aren't going to solve your school problems. So, look at the output, and determine where in your code the error (if there is one) occurred. That is your first step.

VernonDozier and rubberman... Thanks for your replies... But I have found the solution...

I change this code (in line 58 and 108)

void swap(vector<employee> employees, int a, int b);

Into

void swap(vector<employee> &employees, int a, int b);

I found the answer from other forum. It is related to pointers. Can anyone explain me briefly what happened there? What's the logic behind that? Until now I have no clue about pointers. For simple example about pointers, I understand.. When it comes to long code like this one, I'm confused.

So basically it's not method ordering problem or method logic problem... It's pointers >.<

>> I found the answer from other forum.

You should only post a question on one forum at a time. If you are posting the same question in more than one thread or more than one forum, that's "cross-posting". Cross-posting is a big no-no on all forums since it allows people to waste time crafting responses that will never be read to an obsolete question that was already answered elsewhere without their knowledge. If you must cross-post, either close one thread or actually POST the cross-posts in a link on each forum so people on each forum know about the other. Not the end of the world if you didn't know that, but please keep it in mind in the future.


>> It is related to pointers.

It's related to pass-by-reference versus pass-by-value. I guess you could boil it down to "pointers" perhaps, but I wouldn't. I'd boil it down to pass-by-reference versus pass-by-value. The & changed it from the latter to the former. It's this point I made earlier (though dealing with swap rather than sort). The swap works inside the function, but somehow the sort gets "lost" after the function. It gets "lost" because you passed by value. Passing by reference means it doesn't go out of scope (get "lost").

Google "C++ pass-by-reference versus pass-by-value". Lots of good threads explaining the mechanisms and when to use each.

I'm not sure what your teacher wants but some C++ library functions/algorithms can be used to simplify your code. It may be worthwhile to check out a C++ reference before and during your implementation.

>> I found the answer from other forum.

You should only post a question on one forum at a time. If you are posting the same question in more than one thread or more than one forum, that's "cross-posting". Cross-posting is a big no-no on all forums since it allows people to waste time crafting responses that will never be read to an obsolete question that was already answered elsewhere without their knowledge. If you must cross-post, either close one thread or actually POST the cross-posts in a link on each forum so people on each forum know about the other. Not the end of the world if you didn't know that, but please keep it in mind in the future.


>> It is related to pointers.

It's related to pass-by-reference versus pass-by-value. I guess you could boil it down to "pointers" perhaps, but I wouldn't. I'd boil it down to pass-by-reference versus pass-by-value. The & changed it from the latter to the former. It's this point I made earlier (though dealing with swap rather than sort). The swap works inside the function, but somehow the sort gets "lost" after the function. It gets "lost" because you passed by value. Passing by reference means it doesn't go out of scope (get "lost").

Google "C++ pass-by-reference versus pass-by-value". Lots of good threads explaining the mechanisms and when to use each.

Btw that was not me, it is someone else. But thanks for ur advice, I will take that in mind. And thanks for your explanation too about the pointers, it really helps me. Thank you!

I'm not sure what your teacher wants but some C++ library functions/algorithms can be used to simplify your code. It may be worthwhile to check out a C++ reference before and during your implementation.

I guess he wants us to know more about the pointers :p
Because this subject is not about C++, it's more about data structure and algorithms in C++

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.