Problem with my code

Hey guys ..

I have posted my code below.. Im having a small problem when it comes to running the code and getting an answer. there are 5 employees that i pass through to the program via file but in the end i cant seem to sort them in order. I have gone over my code many times but i cant seem to find the problem. Any help would be appreciated. (I did not type in the whole code some was giving to me by my teacher its a small exercise and im new to c++).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#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]); 

             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);
      }
   }
}
It seems that the swap only happen in the function -and the function itself is not functional ;p
try to prototype it like this:
void swapVectorElements(vector<type>& firstElement, vector<type>& secondElement);

...or just use <algorithm> and swap() http://www.cplusplus.com/reference/algorithm/swap/

...or lose the headache and use <algorithm> sort() http://www.cplusplus.com/reference/algorithm/sort/ or http://www.cplusplus.com/reference/algorithm/stable_sort/
Thanx for the reply..

Im not sure if we are allowed to use the algorithm in our code basically because it might be too easy..

Im not quite sure where the first option you included comes in. is that like a new function to the program or is it just modifying the swap method i have ?

I know it seems really noobish to ask these kind of questions but i used this method with an array which was coded in the program and it worked perfectly but when i try to get it from a file it just doesnt work.
Your test result
Jetson 1232 12.56
Cogswall 7165 22.45
Spacelly 5903 27.04
Elroy 123 6.86
Rosie 8080 9.93

Jetson 1232 12.56
Cogswall 7165 22.45
Spacelly 5903 27.04
Elroy 123 6.86
Rosie 8080 9.93


Expected result
Jetson 1232 12.56
Cogswall 7165 22.45
Spacelly 5903 27.04
Elroy 123 6.86
Rosie 8080 9.93

Cogswall 7165 22.45
Elroy 123 6.86
Jetson 1232 12.56
Rosie 8080 9.93
Spacelly 5903 27.04

this is the results and how it should show up.. :)
theonealone wrote:
Im not sure if we are allowed to use the algorithm in our code basically because it might be too easy..

lol! yeah, I guess so... but try it anyways, it's the whole point all these smart people worked really hard to come up with STL, it (STL) is easy to use, intuitive etc.

Im not quite sure where the first option you included comes in. is that like a new function to the program or is it just modifying the swap method i have ?

that's the swap() definition, we can't pass it by value because we want the function to return two values -remember in your previous C++ classes the lecturer talk about that?

here's how swap implemented in STL, look at it (ignore the keyword template/T, they're just fancy stuff, replace T with your class, and you're set)
1
2
3
4
template <class T> void swap ( T& a, T& b )
{
  T c(a); a=b; b=c;
}


happy coding.

edit: remember, swap objects
Last edited on
Thanks alot .. i tried it out .. but i got an error saying hmm

vector.cpp:112: error: two or more data types in declaration of 'swap'...

The code i wrote is :

[code]
class employee void swap ( employee& a, employee& b )
{
//employee temp(employees[a]);
//employees[a] = employees[b];
//employees[b] = temp;

employee c(a); a=b; b=c;
}
[/code[
I tried it without the keyword class also just in case but i got the same error.
Also shouldnt the swap method have 3 parameters because

swap(employees, max, outer);

when that is called it passes 3 arguments. Im just talking with my java knowledge so i might be wrong.
1
2
3
4
5
6
7
8
9
10
void swap ( employee& a, employee& b ) //ignore teh keywords class and type
{
//employee temp(employees[a]);
//employees[a] = employees[b];
//employees[b] = temp;

employee c(a); a=b; b=c;
}
//to call it
swap(first_element, second_element); // simple, no? 


Also shouldnt the swap method have 3 parameters because
swap(employees, max, outer);
when that is called it passes 3 arguments. Im just talking with my java knowledge so i might be wrong.

Well my java knowledge is pretty shallow, so I guess that's how it's done in java.
Try pass a reference to vector<employee> in your original solution
Last edited on
arggghh ..

This c++ is soo confusing..

ok i deleted the class and employee words from the code in the swap function.

and i called the swap function like,

swap(max, outer);

but this still doesnt sort the values but complies.

I have been going over this code for a day now without any success. Im just thinking of giving up and waiting for the solution to be posted so i can see what i did wrong.
Cool it, just try it with a simple test unit first:
1
2
3
4
5
6
7
8
9
employee e1, e2, e3;
e1.setEmployee(/*...*/); // etc...

swap(e1, e2); // test swap and print it

vector<employee> ev;
ev.push_back(ev1); // etc...

sortEmployees(/*etc...*/);



hmmm to be honest im not quite sure how to run that code stuff you just gave.. (I know i suck at c++ programming )

I have this other code which i did and it successfully sort the values. I dont know if that will help but ill just post it here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <string>
#include <iomanip>

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() const {
         cout << fixed;
 cout << setprecision(2) << fixed << left <<  setw(20) << surname << setw(4) << empNumber << "  " << hourlyRate << "\n";
      }
};

void swap(employee employees[], int a, int b);
void sortEmployees(employee employees[], int number);
void printEmployees(employee employees[], int number);

int main()
{
   employee employees[5];

   employees[0].setEmployee("Stone", 35.75, 053);
   employees[1].setEmployee("Rubble", 12, 163);
   employees[2].setEmployee("Flintstone", 15.75, 97);
   employees[3].setEmployee("Pebble", 10.25, 104);
   employees[4].setEmployee("Rockwall", 22.75, 15);

   printEmployees(employees, 5);
   sortEmployees(employees, 5);
   printEmployees(employees, 5);

   return 0;
}

void printEmployees(employee employees[], int number)
{
   int i;

   for (i=0; i<number; i++) {
      employees[i].printEmployee();
   }
   cout << "\n";
}

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

      for (a=a; a<=b; a++)
      {
         if (employees[a].getSurname() > employees[b].getSurname())
        {
             employees[a] = employees[b];
             employees[b] = temp;
        }
      }


}

void sortEmployees(employee employees[], int number)
{
   /* use selection sort to order employees,
      in employee name order
   */

   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 in section of array
         if (employees[inner].getSurname() > employees[max].getSurname())
            max = inner;
      }
      if (max != outer)
      {
         // swap largest with last element looked at in array
         swap(employees, max, outer);
      }
   }
}


well, now I'm stumped -I didn't try to run your code, though. just check in your original solution that you declared
swap(vector<employee>& , int, int)
instead of
swap(vector<employee>, int, int)
Which line number is this on ? i couldnt find that.

And no worries thanx for the help im just going to wait on the solution. I have to look over vectors and sorting again.
Topic archived. No new replies allowed.