If the texPerc
will never be used.
Never output whitespace immediately before a
\n
.
cout<< "Enter the item's price:\n"; float itemPrice; float taxPerc = 1.04375; cin >> itemPrice; if (!cin) { cerr << "The item's price must be a number \n\n"; return EXIT_FAILURE; }
cout << "Enter the item's price:\n"; float itemPrice; //uninitialized variable cin >> itemPrice; if (!cin) { cerr << "The item's price must be a number.\n\n"; return EXIT_FAILURE; } const float taxPerc = 1.04375;
As the weight increases,
the cooking time goes down.
Can’t store
18.75
in an
int
.
Send error messags to
cerr
, then exit.
Bug: if a tiny turkey weighs 3 pounds,
it says “The turkey is too big!”.
int time = 0; // determine the cooking time if (weight >= 4 && weight <= 8) { time = 30; else if (weight >8 && weight <= 8) { time = 20; else if (weight >12 && weight <=16) { time = 18.75; else if (weight > 16 && weight <= 20) { time = 18; else if (weight > 20 && weight <= 24) { time = 17.5; else if (weight > 24 && weight <= 30) { time = 12.5; else cout << "The turkey is too big!\n";
double time; //cooking time in minutes; uninitialized variable if (weight < 4) { time = 0; else if (weight <= 8) { time = 30; else if (weight <= 16) { time = 18.75; else if (weight <= 20) { time = 18; else if (weight <= 24) { time = 17.5; else if (weight <= 30) { time = 12.5; else { cerr << "The turkey is too big!\n"; return EXIT_FAILURE; }
Easier to maintain with an array of structures.
struct category { double pounds; double minutes; }; const category a[] = { { 4, 0}, { 8, 30}, {16, 18.75}, {20, 18}, {24, 17.5}, {30, 12.5} }; const size_t n = sizeof a / sizeof a[0]; double time; //cooking time in minutes; uninitialized variable for (size_t i = 0; i < n; ++i) { if (weight <= a[i].pounds) { time = a[i].minutes; goto done; } } cerr << "The turkey is too big!\n"; return EXIT_FAILURE; done:;