Help with exchange sort strings

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.


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
#include <iostream>
#include <string.h>
#include <string>

using namespace std;

int main()
{

	struct sales
	{
		
		float sales_figure;
		char last_name[21];
	}entry[10];

	int i,j,n, 
	counter=0;

	for (counter;counter <= 9;counter++)
	{
		
		cout << "Please enter your last name:\n";  //Asks for user`s last name
		cin >> entry[counter].last_name;
		cout << endl;
		if (strcmp(entry[counter].last_name, "quit") == 0)
			break;  //exit loop
		cout << "Please enter the amount of sold?" << endl;
		cin >> entry[counter].sales_figure;
		system("cls");
	}

	for (i = 0;i <= 8;i++)
	{
		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;
			}
		}
		for (i = 0;i <= counter;i++)
		{
			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";
		}
		system("pause");
	}
}



---------------------------------------------------PROBLEM-------------------------------------------------

{
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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.
Again, the curly brackets which start at 34 must end at 57 but it ends at 65 in your program.
THis should sort it properly(sales_figure and names)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (i = 0;i <= 8;i++)
         { 
	     for (j = i + 1;j <= 9;j++)
              {
               sales temp;
               if (entry[i].sales_figure < entry[j].sales_figure)     //sorting the values of the sales_figure array
		      {
		       temp = entry[i];
		       entry[i] = entry[j];
		       entry[j] = temp;
		      }
              }

	}
Last edited on
Thank you. That helped a lot.
DEnforcer You are welcome.
Topic archived. No new replies allowed.