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