Hi this is my assignment for my programming II class. I am to create a data structure that stores the last name and sales figure in an array for their respected datatypes. The user is supposed to be able to enter a maximum of up to 10 last names and sale figures to be arranged from the highest to lowest. I am able to sort the integer values in the array no problem, but I am having the hardest time trying to figure out how to sort the last name strings in the string array that correspond with each integer sales figure in the correct order.I heard it may have something to do the strcmp function, but I don`t understand how it can be used to rearrange string values in the string array. Here is my code I have so far.
EDIT:I apologize if i misused my code tags.
{
cout << "Last name:" << entry[i].last_name << "\n\nTotal Sales figures (Highest to Lowest)\n";
cout << "$" << entry[i].sales_figure << endl; //output is shown from highest to lowest
cout << "\n";
}
Note: When it displays last name, it will always display it in the exact order that the user typed them in. Theres no way that I know how to sort it with its sales figure.
for (j = i + 1;j <= 9;j++)
{
int temp;
if (entry[i].sales_figure < entry[j].sales_figure) //sorting the values of the sales_figure array
{
temp = entry[i].sales_figure;
entry[i].sales_figure = entry[j].sales_figure;
entry[j].sales_figure = temp;
}
}
for (j = i + 1;j <= 9;j++)
{
int temp;
if (entry[i].sales_figure < entry[j].sales_figure) //exchange sorting the values of the sales_figure array
{
temp = entry[i].sales_figure;
entry[i].sales_figure = entry[j].sales_figure;
entry[j].sales_figure = temp;
}
}
Why did you repeat it twice?
Anyway, the problem is that, you are indeed swapping the sales_figure, but not swapping the names!
So, when entry[i].sales_figure < entry[j].sales_figure , swap the names too.