overloaded operator with <() function

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
/*
Title		: Lab Task 4 Question 3
Author		: Lim Boon Jye
Description	: Operator Overloading
Date		: 13 September 2012
*/
#include <iostream>
#include <string>

using namespace std;

class Jobbid{
public:
	int bidNum ;
	double quotedPrice;

	//Jobbid();
	//~Jobbid();
	//void displayLowest();

	bool operator < (const Jobbid& j) const {
		if( quotedPrice < j.quotedPrice ){
			return true;
		}
		if( quotedPrice > j.quotedPrice ){
			return false;
		}
	}
     //int Bid() { return bidNum; }
      //int Qoute() { return quotedPrice; }

};

//void Jobbid :: displayLowest(){
//
//}

int main()
{
	Jobbid thejobbid[4];
	int arrayOfbid[4];
	double priceQuoted[4];

	for( int i = 0 ; i < 4 ; i++ ){
		cout << "Enter Bid Number : ";
		cin  >> thejobbid[i].bidNum;

		cout << "Enter quote price: ";
		cin  >> thejobbid[i].quotedPrice;;
	}

	for( int i = 0 ; i < 4 ; i++ ){
		if ( thejobbid[i].quotedPrice < thejobbid[i+1].quotedPrice ){
			cout << thejobbid[i+1].bidNum << " more than " << thejobbid[i].bidNum <<endl;
		}
		else if ( thejobbid[i].quotedPrice > thejobbid[i+1].quotedPrice ){
			cout << thejobbid[i+1].bidNum << " less than " << thejobbid[i].bidNum <<endl;
		}
	}
	cin.ignore();
	cin.get();
	return 0;
}


My code is wrong . can any senior try to help?
I've to create 4 array of the Object
I have to display the lowest Quoted price with their Bid Number ID
Have to use overloaded operator <()
Cannot use constructor to set field values

so , where my code wrong?

My sample out
1
2
3
4
5
6
7
8
9
10
11
12
Enter Bid Number : 0413
Quoted Price     : 50.00
Enter Bid Number : 0414
Quoted Price     : 50.05
Enter Bid Number : 0415
Quoted Price     : 60.00
Enter Bid Number : 0416
Quoted Price     : 20.00


Lowest Price Quote is : RM 20
Bid Number ID is      : 0416


but I not really sure how to do it.. need to use overloaded array
Stop creating 100 threads about the same thing!

Your operator< has a problem because it doesn't return anything if the two quotedPrice is equal. There isn't much point using an if statement at all. Just return the value of quotedPrice < j.quotedPrice directly.


To find the lowest price you have to loop through all objects in the array. Keep track of the object with the lowest price so far. When you find a price that is lower than the lowest so far you set that as the new lowest so far. When you have looped through all the objects the lowest so far is also the lowest of them all.
@peter

sorry , i won't make it again.

by the way . I thought i already return the value as true and false?
i can't get what mean by directly.. sorry , because i search a lot example they provide me the true and false only..

and i thought i already looping and search the lowest 1 ? which part that my coding are wrong? sorry again and sorry
I mean that if quotedPrice == j.quotedPrice the function doesn't return anything. By "directly" I mean:
1
2
3
bool operator < (const Jobbid& j) const {
	return quotedPrice < j.quotedPrice;
}


The loop is not checking for the lowest one. It just compares each element with next/previous object. That loop also has the problem that thejobbid[i+1] is out of bounds when i == 3.
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
#include <iostream>
#include <string>

using namespace std;

class Jobbid{
public:
	int bidNum ;
	double quotedPrice ;

	bool operator < (const Jobbid& j) const {
		return quotedPrice < j.quotedPrice;
	}

};

int main()
{
	Jobbid thejobbid[4];
	int arrayOfbid[4];
	int highest , lowest ;
	double  large , small ;
	large = thejobbid[0].quotedPrice = 0;
	small = thejobbid[0].quotedPrice = 0;

	highest = thejobbid[0].bidNum = 0;
	lowest  = thejobbid[0].bidNum = 0;

	for( int i = 0 ; i < 4 ; i++ ){
		cout << "\nEnter Bid Number : ";
		cin  >> thejobbid[i].bidNum;

		cout << "Enter quote price: ";
		cin  >> thejobbid[i].quotedPrice;
	}

	for(int i = 1 ; i < thejobbid[i].bidNum ; i++ ){
		if(thejobbid[i].quotedPrice > large ){
		large = thejobbid[i].quotedPrice;
		highest = thejobbid[i].bidNum;
		}
		if(thejobbid[i].quotedPrice < small ){
		small = thejobbid[i].quotedPrice;
		lowest = thejobbid[i].bidNum;
		}
	}

	cout << "\nLowest Job Bid is  : " << small  << endl;
	cout << "The Bid Number ID is : " << lowest << endl;
	
	cout << "\nHighest Job Bid is  : " << large  << endl;
	cout << "The Bid Number ID is : " << highest << endl;

	cin.ignore();
	cin.get();
	return 0;
}


Sample output :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Enter Bid Number : 1111
Enter quote price: 50

Enter Bid Number : 2222
Enter quote price: 40

Enter Bid Number : 3333
Enter quote price: 20

Enter Bid Number : 4444
Enter quote price: 10

Lowest Job Bid is  : 0
The Bid Number ID is : 0

Highest Job Bid is  : 40
The Bid Number ID is : 2222


it's print to wrong place . how come?
Last edited on
On lines 23-27 you set the variables to zero.
if i no set the variable to zero.

it state the my thejobbid is use without initialize..
No Jobbid has quotedPrice < 0 so small and lowest will never be updated in the loop.
You also miss to check thejobbid[0] because you start at i == 1.
okay from here.
i able to do the highest already . but the lowest value cannot.
it still out

0
and alien address.

why?
There are such standard algorithms as std::max_element, std::min_element and std::minmax_element. So you can use them to find the minimum and maximum in your array.
Last edited on
can provide me the example ?

i never heard about this . sorry
#include <algorithm>

Jobbid *minimum = std::min_element( thejobbid, thejobbid + 4 );

std::cout << minimum->quotedPrice << minimum->bidNum << std::endl;

Or

std::pair<Jobbid *, Jobbid *> p = std::minmax_element( thejobbid, thejobbid + 4 );

std::cout << p.first->quotedPrice << p.first->bidNum
<< p.second->quotedPrice << p.second->bidNum << std::endl;
You should try to solve it without <algorithm> because you will probably learn something from it.
got another choice to do so ?

since i not yet learn the algorithm .
i have to use the knowledge that in my area .

using the comparison between object ?
To find the highest and lowest elements let assume that the first element of the array is correspondingly the highest and the lowest element of the arrray.


Jobbid highest = thejobbid[0];
Jobbid lowest = thejobbid[0];

Then in a loop you compare these elements ( highest and lowest ) with others to find the highest and lowest. After that you display their members.
Last edited on
@vlad

thanks for ur help , and i fix it too.
i just change the [0] before the looping and it's work now.

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

using namespace std;

class Jobbid{
public:
	int bidNum ;
	double quotedPrice ;

	bool operator < (const Jobbid& j) const {
		return quotedPrice < j.quotedPrice;
	}

};

ostream& operator<<(ostream& out, Jobbid& outbid){
	out << "\nLowest Job bid"<<endl;
	out << "-----------------------"<<endl;
	out << "The Bid Number ID is : " << largest << endl;
	out << "The Quoted price is  : " << outbid.quotedPrice  << endl;

	out << "\n\nHighest Job bid"<<endl;
	out << "-----------------------"<<endl;
	out << "The Bid Number ID is : " << outbid.bidNum << endl;
	out << "The Quoted price is  : " << outbid.quotedPrice   << endl;
	return out;
}

istream& operator>>(istream& in, Jobbid& inbid){
	cout << "\nEnter Bid Number : ";
	cin  >> inbid.bidNum;

	cout << "Enter quote price: ";
	cin  >> inbid.quotedPrice;

	return in;
}

int main()
{
	Jobbid thejobbid[4];
	int arrayOfbid[4];
	int highest , lowest ;
	double  large , small ;

	for(int i = 0 ; i < 4 ; i++ ){
		thejobbid[i].quotedPrice = 0;
	}

	for( int i = 0 ; i < 4 ; i++ ){;
		cin  >> thejobbid[i];
	}

	large = thejobbid[0].quotedPrice  ;
	small = thejobbid[0].quotedPrice ;

	highest = thejobbid[0].bidNum ;
	lowest  = thejobbid[0].bidNum ;

	for(int i = 0 ; i < 4 ; i++ ){
		if(thejobbid[i].quotedPrice < small ){
			small = thejobbid[i].quotedPrice;
			lowest = thejobbid[i].bidNum;
		}
		if(thejobbid[i].quotedPrice > large ){
			large = thejobbid[i].quotedPrice;
			highest = thejobbid[i].bidNum;
		}
	
	}

	/*cout << "\nLowest Job bid"<<endl;
	cout << "-----------------------"<<endl;
	cout << "The Bid Number ID is : " << lowest << endl;
	cout << "The Quoted price is  : " << small  << endl;
	
	
	cout << "\n\nHighest Job bid"<<endl;
	cout << "-----------------------"<<endl;
	cout << "The Bid Number ID is : " << highest << endl;
	cout << "The Quoted price is  : " << large   << endl;*/

	for( int i = 0 ; i < 4 ; i ++){
	cout << thejobbid[i];
	}

	cin.ignore();
	cin.get();
	return 0;
}


last , i wan to ask how should i put my code in
1
2
3
4
5
6
7
8
9
10
11
12
ostream& operator<<(ostream& out, Jobbid& outbid){
	out << "\nLowest Job bid"<<endl;
	out << "-----------------------"<<endl;
	out << "The Bid Number ID is : " << largest << endl;
	out << "The Quoted price is  : " << outbid.quotedPrice  << endl;

	out << "\n\nHighest Job bid"<<endl;
	out << "-----------------------"<<endl;
	out << "The Bid Number ID is : " << outbid.bidNum << endl;
	out << "The Quoted price is  : " << outbid.quotedPrice   << endl;
	return out;
}


i have to use istream operator >> show it out. but for the lowest and highest price , i cannot put small or highest already since it's not a same place , so , what should i out insid emy istream operator?
Topic archived. No new replies allowed.