/*
Title : Lab Task 4 Question 3
Author : Lim Boon Jye
Description : Operator Overloading
Date : 13 September 2012
*/
#include <iostream>
#include <string>
usingnamespace std;
class Jobbid{
public:
int bidNum ;
double quotedPrice;
//Jobbid();
//~Jobbid();
//void displayLowest();
booloperator < (const Jobbid& j) const {
if( quotedPrice < j.quotedPrice ){
returntrue;
}
if( quotedPrice > j.quotedPrice ){
returnfalse;
}
}
//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;
}
elseif ( 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
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.
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
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.
#include <iostream>
#include <string>
usingnamespace std;
class Jobbid{
public:
int bidNum ;
double quotedPrice ;
booloperator < (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
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.
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.
#include <iostream>
#include <string>
usingnamespace std;
class Jobbid{
public:
int bidNum ;
double quotedPrice ;
booloperator < (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?