Hello. I am beginner using C++.
I have been searching through previous posts, but I haven't found anything to apply to my problem...
I have a program that I initialize data into an array. I am using a Structure. After initializing the data, I use a Switch statement to allow the user to select if they want to search by last name, id number or pay rate.

Now I need to add a bubble sort to sort the data based on the item that they chose to search by.

I have an example of a bubble sort, but I dont' understand how to appy it to my program because of the STRUCTURE. I don't know how to name the items using the dot operator. I would appreciate any assistance given.

Here is my code (not including the bubble sort):

#include <iostream>


using namespace std;

 // Declare Employee structure
   struct Employee
   {
    int idNum;
    char firstName[20];
    char lastName[20];
    double payRate;
   };
 
 

int main()
{
   //Declare variables for Main
   int searchby;
   int idNumber;
   char lname[20];
   double prate;
   int i;
   int truetest = 1;
    
   // Employee Data  
   Employee workers[5] = {{216, "William", "Fullerton", 13.50}, 
                          {150, "Ashley", "Ross", 14.00}, 
                          {78, "Sharon", "Webb", 17.20}, 
                          {180, "George", "Ross", 16.80}, 
                          {10, "Eli", "Manning", 35.00}};
    
                                 
   cout << "Enter the corresponding number To Search By: \n\n(1)for ID Number";
   cout << " \n(2)for Last Name \n(3)for Hourly Pay\n\n";

   cin >> searchby;

   // Begin of Switch Statement
   switch(searchby)
      {
      case 1:
           cout <<  "\n\n\tPlease enter the ID Number to search  \n\n";
           cin >> idNumber;
           for (i=0; i<5; i++)
               if (idNumber == workers[i].idNum)
               {
                  cout << "\n\n" << workers[i].idNum << "\t" 
                  << workers[i].firstName << " " << workers[i].lastName 
                  << "\t\t" << workers[i].payRate;
                  truetest = 0;
               } 
               if (truetest == 1)   //test to see if search criteria was found
                  cout << "\n\n\tThe ID Number Entered was NOT found!\n\n";
               break;
           
       case 2:
           cout <<  "\n\n\tPlease enter the Last Name to search  \n\n";
           cin >> lname;
           for (i=0; i<5; i++)
               if (strcmp(lname, workers[i].lastName)==0)
                 
               {
                  cout << "\n\n" << workers[i].idNum << "\t" 
                  << workers[i].firstName << " " << workers[i].lastName 
                  << "\t\t" << workers[i].payRate;
                  truetest = 0;
               } 
               if (truetest == 1)   //test to see if search criteria was found
                  cout << "\n\n\tThe Last Name Entered was NOT found!\n\n";
               break;

       case 3:
           cout <<  "\n\n\tPlease enter the Pay Rate to search  ";
           cout <<  "\n\n\t(You Must Enter the Exact Amount\n";
           cout <<  "\n\n\t(Example: 17.20 or 14.00)\n\n";
           cin  >> prate;
           for (i=0; i<5; i++)
               if (prate == workers[i].payRate)
               {
                  cout << "\n\n" << workers[i].idNum << "\t" 
                  << workers[i].firstName << " " << workers[i].lastName 
                  << "\t\t" << workers[i].payRate;
                  truetest = 0;
               }
               if (truetest == 1)   //test to see if search criteria was found
                   cout << "\n\n\tThe Pay Rate Entered was NOT Found!\n\n";
               break;

       }   

    cout << " \n\n ";
    system ("\n\nPAUSE");
    return 0;
}

Here is my example of the bubble sort: I just don't know how to change the variables to correctly apply to the array names and elements:

for (j=1; j<6; j++)
        {
       for (i=0; i<5; i++)
       {
           if n[i] > n[i+1])
           {
             temp = n];       
             n[i] = n[i+1];
             n[i+1] = temp;
             
           }
       }
    }

I have an example of a bubble sort, but I dont' understand how to appy it to my program because of the STRUCTURE. I don't know how to name the items using the dot operator. I would appreciate any assistance given.

If you don't know how to access members of a struct yet, then you shouldn't be trying to implement any sort of sorting algorithm. You need to learn the ultra basics first. The dot operator is a good place to start.

Say you have this struct

struct SomeContainer{
    int integerVal;
    double decimalVal;
};

Then, accessing its elements is quite trivial:

int main(){
    SomeContainer box = { 31, 56.96124843 };
    int theInteger = box.integerVal;  // This will now equal 31
    double theDecimal = box.decimalVal;  // This will now equal  56.96124843
    SomeContainer boxArray[3] = { { 1, 0.0 }, { 2, 0.6931 ), ( 3, 1.0986 } };
    theInteger = boxArray[1].integerVal;  // This will now equal 2
    theDecimal = boxArray[2].decimalVal;  // This will now equal 1.0986
    return 0;
}

That should get you started

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.