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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int num;
//functions implemented for you
//Requires: nothing
//Modifies: nothing
//Effects: prints out the initial header
void printHeader();
//Requires: num is 1-4 inclusive
//Modifies: nothing
//Effects: prints the appropriate error message
// groups printing of error messages together into a function
void printErrorMessage(int num);
//Needs to be implemented
//Requires: prompt is non-empty
//Modifies: nothing
//Effects: prints the 'prompt' ;
// reads the price;
// dumps all data that remains on the line
// returns the price
double getPrice(double reservePrice);
double getPrice()
{
double reservePrice;
cout << "Reserve price: $ ";
cin >> reservePrice;
string a;
getline(cin,a);
return reservePrice;
}
//Requires: bidPrice and reservePrice to be loaded
// reservePrice > 0
//Modifies: nothing
//Effects: returns true if the bidPrice is >=reservePrice
// false otherwise
// Also, prints errorMessage 2 if bidPrice < 0
// prints errorMessage 1 if bidPrice < reservePrice
bool isBidPriceGood(double bidPrice, double reservePrice);
bool isBidPriceGood(double bidPrice,double reservePrice)
{
if (reservePrice > 0)
{
if (bidPrice >= reservePrice)
{
return true;
}
else if (bidPrice < 0)
{
printErrorMessage(2);
return 0;
}
else if (bidPrice < reservePrice)
{
printErrorMessage(1);
return 0;
}
else
{
printErrorMessage(5);
return 0;
}
}
return 0;
}
//Requires: prompt is non-empty
//Modifies: nothing
//Effects: prints the 'prompt'
// reads the name of the bidder and returns this name
string getName(string bidder1);
string getName2(string bidder2);
string getLot(string lotName);
string getLot()
{
string lotName;
cout << "Enter lot name: ";
getline(cin,lotName);
return lotName;
}
string getName()
{
string bidder1;
cout << "Bidder1 ID: ";
getline(cin,bidder1);
return bidder1;
}
string getName2()
{
string bidder2;
cout << "Bidder2 ID: ";
getline(cin,bidder2);
return bidder2;
}
double getBid1(double bid1);
double getBid1()
{
double bid1;
cout << "Bidder1 price: $ ";
cin >> bid1;
if (bid1 <= 0)
{
int num = 2;
}
string f;
getline(cin,f);
return bid1;
}
double getBid2(double bid2);
double getBid2()
{
double bid2;
cout << "Bidder2 price: $ ";
cin >> bid2;
string u;
getline(cin,u);
return bid2;
}
//Requires: bidder is non-empty, lotName is non-empty, bid is valid
//Modifies: nothing
//Effects: prints out "Congratulations", the winning bidder,
// "You won", the name of the lot along with the price
//Example:
//Congratulations, Hobbes002
//You won "1987 Sugar Bombs" at a price of $2.25
void printWinner(string bidder, string lotName, double bid);
void printWinner(string bidder, string lotName, double bid)
{
cout << "Congratulations, " << bidder << endl;
cout << "You won " << "\"" << lotName << "\"" << " at a price of $" << bid;
}
//Requires: all parameters to be loaded
// reservePrice > 0
//Modifies: nothing
//Effects: will calculate the winner of the auction and print out the winner
//Note: you have a function that does the printing
// also, prints error message in case no-one won
void calcWinner(string bidder1, string bidder2, string lotName,
double bid1, double bid2, double reservePrice);
void calcWinner(string bidder1, string bidder2, string lotName, double bid1, double bid2, double reservePrice)
{
double winningbid;
if (bid1 && bid2 >= reservePrice)
{
if (bid1 > bid2)
{
winningbid = bid2 + 0.5;
printWinner(bidder1, lotName, winningbid);
}
else if ((bid2 >= bid1) && (bid2 < bid1 + 0.5))
{
winningbid = bid1;
printWinner(bidder1, lotName, winningbid);
}
else if (bid2 >= bid1 + .5)
{
winningbid = bid1 + 0.5;
printWinner(bidder2, lotName, winningbid);
}
}
else if (bid2 >= reservePrice)
{
winningbid = bid2;
printWinner(bidder2, lotName, winningbid);
}
else if ((bid2 < reservePrice) && (bid1 >= reservePrice))
{
winningbid = reservePrice;
printWinner(bidder1, lotName, winningbid);
}
else if (bid1 && bid2 < reservePrice)
{
printErrorMessage(4);
}
}
void printHeader()
{
cout << "----------------------------------------" << endl
<<" bCreek" << endl
<<" Fine Cereal Sales" << endl
<<"----------------------------------------" << endl << endl;
cout << fixed << showpoint;
cout << setprecision(2);
}
int main(double bid1, double bid2, double reservePrice, string bidder1, string bidder2)
{
double a,b,c;
string g,h,i;
printHeader();
i = getLot();
cout << endl;
a = getPrice();
if (a < 0)
{
printErrorMessage(5);
return 0;
}
cout << endl;
h = getName();
if (h == "")
{
printErrorMessage(3);
cout << endl;
g == getName2();
cout << endl;
c = getBid2();
isBidPriceGood(c,a);
calcWinner(h, g, i, 0, c, a);
return 0;
}
cout << endl;
b = getBid1();
isBidPriceGood(b,a);
if (b >= a)
{
cout << h << " is high bidder, current price = $" << a;
}
cout << endl;
g = getName2();
if (g == "")
{
printErrorMessage(3);
cout << endl;
calcWinner(h, g, i, b, 0, a);
return 0;
}
cout << endl;
c = getBid2();
isBidPriceGood(c,a);
calcWinner(h, g, i, b, c, a);
return 0;
}
void printErrorMessage(int num)
{
if (num == 1) {
cout << endl
<< " ERROR: Reserve not met, bid rejected" << endl << endl;
} else if (num == 2) {
cout << endl
<< " ERROR: Negative price, bid rejected" << endl << endl;
} else if (num == 3) {
cout << endl
<< " ERROR: Blank bidder ID, no bid allowed" << endl << endl;
} else if (num == 4) {
cout << endl
<< "ERROR: Neither bidder met Reserve, auction canceled" << endl << endl;
} else if (num == 5) {
cout << endl
<< "ERROR: Reserve is not positive, auction canceled" << endl << endl;
} else {
cout << " This should never print" << endl << endl;
}
}
|