Logic Error Help

This is a discussion on Logic Error Help within the C++ Programming forums, part of the General Programming Boards category; I'm trying to sort the below by last name from A -> Z but I can't get it to sort ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Logic Error Help

    I'm trying to sort the below by last name from A -> Z but I can't get it to sort and I'm not sure where I've buggered it. I've tried desk checking it but it all looks ok to me. Any ideas would be great thanks.

    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 printEmployee() {
             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].printEmployee();
       }
       printf("\n");
    }
    
    void swap(vector<employee> employees, int a, int b)
    {
       employee temp(employees[a]);
    
       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);
          }
       }
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Your swap function is working on a copy of the vector, so that in itself prevents your sortEmployees from sorting.

    I also noticed that this line
    Code:
       temp = employees[a];
    is extraneous since temp is already defined as employees[a].

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Is that because of the declare at the start of the function or somewhere else?

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Yes, the first parameter should be
    Code:
    vector<employee> &employees
    as in your other functions.

    Why did you make the swap() function different from the others?

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    I didn;t intentionally I just missed it for some stupid reason.

    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 12:05 PM
  2. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 05:41 PM
  3. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 10:58 PM
  4. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21