Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7689845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:15:33+00:00 2026-05-31T20:15:33+00:00

i am new to C++ and stuck in the swap stuff the code below

  • 0

i am new to C++ and stuck in the swap stuff
the code below is a program of sort employee names in alphbetical order and print out the orginal one and sorted one ,but the
swap method doesn’t work
the two output of printEmployees is excatly the same, can anyone help me? thx

    #include <iostream>

    #include <string>

    #include <iomanip>

    #include <algorithm>
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){}

      void setEmployee(const string &name, double rate,int num);
      string getSurname() const;
      void printEmployee() const;
     employee& operator = (const employee &other)
    {employee temp(other);
     return *this;}};    

     void employee::setEmployee(const string &name, double rate, int num) {   
      surname = name;
      hourlyRate = rate;
      empNumber = num;
      }
     string employee::getSurname() const { return surname; }  
     void employee::printEmployee() const {
         cout << fixed;
         cout << setw(20) << surname << setw(4) << empNumber << "  " << hourlyRate << "\n";
      }

  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]);
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);
      }
   }
}


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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:15:34+00:00Added an answer on May 31, 2026 at 8:15 pm

    As told by others, fixing your assignment operator will solve the problem.
    I see that you tried to implement operator= in terms of copy constructor but missed to do a swap. You can try the below approach if you want to avoid code duplication in your copy constructor and assignment operator.

    employee& operator=(const employee& other)
    {
        employee temp(other);
        swap(temp);
        return *this;
    }
    
    void swap(employee& other)
    {
         std::swap(surname, other.surname);
         std::swap(hourlyRate, other.hourlyRate);
         std::swap(empNumber, other.empNumber);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to wicket and stuck with the following problem: I have a table
When working on developing new software i normally get stuck with the redundancy vs
I am pretty new to the Unity Application Block and am a little stuck
I am new to php and trying to write a login function. Bit stuck
I am new to C++ and STL. I am stuck with the following simple
I have written this code but it will print these stack traces in the
I have structure like this below. Now, I want swap 2 structures. public struct
New to pthread programming, and stuck on this error when working on a C++&C
I'm stuck with this Java homework question: Write a new method, Refund, that simulates
I'm new to objective-c programming and I stuck. I have uislider in my viewcontroller

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.