Problems with vector sort

Im having trouble with vector sort, problems are in comments in code. Please let me know if more information is needed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// function declaration 
bool sort_price(const Wine& price1,const Wine& price2);

// function definition
//giving error "Lab3.h:117:1: warning: control reaches end of non-void function [-Wreturn-type]"
// from my understanding this error happens when there is no return statement,
// but I have one included. Not sure what the problem is
 bool sort_price(const Wine& price1, const Wine& price2)
{ 
	return price1.get_price() < price2.get_price(); 
}

// function implimintation
// (I believe ive fixed errors here
sort(wines.begin(), wines.end(), sort_price);
	
		int j = 0;
		while (j <=wines.size()){
			j++;
		wines[j]->print();
	}
Last edited on
Please post a small complete program that illustrates your problem.

The sort compare function looks to be correct.

I really don't understand what you're trying to do with that for/while loop but that part of your code really doesn't look correct.
Sorry! line 16 (for loop) was not supposed to be in there, that was from an older program where I'm borrowing this sort function.

as for the while loop, I'm using that to print out the sorted data.

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
int main () {

string wn;
printSignature();

vector<WineryName*> winName;
int street_number, zip_code;
string street, city, state, country;
	bool more = true;
	// loop to initilize winerys
while (more)
{
	cout << "Please enter the winery's name: (no to quit)";
	cin >> wn;
		if (wn == "no") 
		{
		more = false;
		}

	else
	{
		cout << "Please enter the street number: ";
		cin >> street_number;
		cout << "Please enter the street : ";
		cin.ignore();
		getline(cin,street);
		cout << "Please enter the city : ";
		getline(cin,city);
		cout << "Please enter the state : ";
		getline(cin, state);
		cout << "Please enter the zip code : ";
		cin >> zip_code;
		cout << "Please enter the country : ";
		cin.ignore();
		getline(cin, country);
	winName.push_back(new WineryName(wn, street_number, street, city, state, zip_code,country));
	}
}
cout << endl; 
	// loop to initilize winerys ends
	
	// loop to initilize wines starts
vector<Wine*> wines;
string n, wt;
int sc, vin;
double p;
bool more1 = true;
while (more1) {

cin.ignore();
	cout << "Enter wine name " ;
	getline (cin, n);

	if ( n == "no") more1 = false;
	else
	{
	
	cout << "Enter the wine type: ";
	getline (cin, wt);
	cout << "Enter the wine vintage: ";
	cin >> vin;
	cout << "Enter the wine score: ";
	cin >> sc;
	cout << "Enter the wine price: ";
	cin >> p;
	Wine* wine = new Wine(n);
		cout << "enter the winerys name:";
	string wineryname;
	cin.ignore();
	getline (cin, wineryname);
	
	wine->setWineryName(find(winName, wineryname));
	wine->setWineInfo(n, p, sc);
	wine->setWineVintage(vin);
	wine->setWineType(wt);
	wines.push_back(wine);
	}
}

cout << "These are the wines sorted by vintage: " << endl;
sort(wines.begin(), wines.end(), sort_price);

		int j = 0;
		while (j <= wines.size())
		{
			j++;
		wines[j]->print();
	}

return 0;
}
Topic archived. No new replies allowed.