Pointers and the BubbleSort

I am getting an error when I use a pointer in part of my bubble sort. Can anyone help? The error occurs on lines 122 & 123 at the first open parentheses. It says expression must be a modifiable lvalue.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

//student structure
struct Student
{
	string name;
	double score;
};

//function prototypes
void blanklines(int count);
void sortAscending(Student *pStudent, int size);
double Average(Student *pStudent, int size);

 int main()
 {
	 Student test;
	 Student *pStudent;
	 pStudent = &test;
	
	 //variables
	 double average;
	 int numScores;
	 double min = 0;
	 double max = 100;

	//title
	cout << "\t\t\t\tClass Average";
	cout << endl;				// Terminates the title and moves to new line

	blanklines(3);				// Output 3 blanklines

	//get scores
	cout << "How many test scores? ";
	cin >> numScores;

	blanklines(2);

	//error loop
	while(numScores <= 0)
	{
		cout << "Please enter a number greater than 0: ";
		cin >> numScores;
		cout << endl << endl;
	}

	//allocate memory
	pStudent = new Student[numScores];

	//enter names and test scores
	for(int i = 0; i < numScores; i++)
	{
		system("cls");
		blanklines(4);
		cout << "Please enter the name of Student " << i + 1;
		getline(cin, (*pStudent).name);
		cout << "\t\tPlease enter the score for " << (*pStudent).name[i]
			<< i + 1 << ": ";
		cin >> (*pStudent).score;

		//error loop
		while((*pStudent).score < min || (*pStudent).score > max)
		{
			cout << "\n\nPlease enter a number greater than or equal to 0\n"
				<< "or less than or equal to 100: ";
			cin >> (*pStudent).score;
			cout << endl << endl;
		}
	}

	//call sort
	sortAscending(&test, numScores);

	system("cls");

	//title
	cout << "\t\t\t\tClass Average";
	cout << endl;				// Terminates the title and moves to new line

	blanklines(3);				// Output 3 blanklines

	//display scores
	cout << "Your test scores are:\n";
	for(int i = 0; i < numScores; i++)
	{
		cout << "  " << ((*pStudent).score + i);
		cout << endl;
	}

	//display average
	blanklines(1);
	cout << "Your average score is " << Average(&test, numScores);

	//seperate output from environment
	blanklines(10);

	//free allocated mem
	delete [ ] pStudent;
	pStudent = 0;
	return 0;
 }

 //function sortAscending
 void sortAscending(Student *pStud, int size)
 {
	 int templarge;
	 int tempsmall;
	 bool swap;

	 do
	 { swap = false;
	 for (int count = 0; count < (size - 1); count++)
	 {
		 if (((*pStud).score + count) > ((*pStud).score + (count + 1)))
		 {
			 templarge = ((*pStud).score + count);
			 tempsmall = ((*pStud).score + (count + 1));
			 ((*pStud).score + count) = tempsmall;
			 ((*pStud).score + (count + 1)) = templarge;
			 swap = true;
		 };
	 }
	 } while (swap);
 }

 //function average
 double Average(Student *pStudent, int size)
 {
	 double sum = 0;
	
	 for (int i = 0; i < size; i++)
		 sum = sum + ((*pStudent).score + i);

	 return sum / size;
 }

 void blanklines(int count)
 {
	 int i;		// Counter

	// Output blanklines
	for (i = 0; i < count; i++)
		cout << endl;
 }
sorting objects is different than sorting single data types. ill use an object Card as an example:
so we have our class here
1
2
3
4
5
class Card{
public: //do not ussually do that
         int value;
         char suit; 
};

so now we need our vector(or array) of Cards.
std::vector<Card> deck;
now that we have that lets use the std::sort function on the algorithms library.
for this we need 2 random access iterators and a compare class function. the function will look like this

1
2
3
4
5
6
7
8
9
bool sortorder(const card &a, const card &b){
    if(a.suit < b.suit) 
        return true; 
    if(a.suit == b.suit){
        if(a.value < b.value)
            return true;
    }
    return false; 
}


so now lets plug it in.
std::sort(deck.begin(), deck.end(), sortorder); and there we go. it is now properly sorted.
i hope this helps for how to sort objects.
also please remove your system commands please.
Last edited on
Topic archived. No new replies allowed.