Hello HowieW,
As it happens I sometimes end up with a responce that is larger than can fit in one message, so this is in two parts.
zapshe has made very good points here.To expand the compiler does not care about white space or blank lines when it compiles your program.They are just ignored.On the other hand someone reading your code, including your - self, does care about how it looks.So the proper indenting along with a judicious use of blank lines makes the code easier to read and understand.Since you are writing the code for someone to read, especially when you want help, make it as easy to understand as possible.
And now is the time to learn a better way of coding.Not later when bad habits are hard to change.
Here is your program made to work.Not the best way to go about it, but I did manage to get it to work.Be sure to read the comments I put in the program.
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
|
#include <iostream>
#include <iomanip>
#include <limits>
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
int main()
{
const double
WEIGHT_MIN = 1.0,
WEIGHT_MAX = 50.0,
BIG_MIN = 1.0,
BIG_MAX = 1000.0,
MIN_CHARGE{ 1.5 },
MAX_CHARGE{ 2.5 },
DIVISOR{ 5.0 },
CHARGE_WEIGHT{ 25.0 };
const double TAX_RATE{ 0.05 };
double package_weight{},
big{},
total_charges{},
length{},
width{},
depth{};
std::cout << std::fixed << std::showpoint << std::setprecision(2); // <--- Only needs done once.
std::cout << "\n What is the weight (LBS) of the package? ";
std::cin >> package_weight;
while (!std::cin || (package_weight < WEIGHT_MIN || package_weight > WEIGHT_MAX))
{
if (!std::cin)
{
std::cout << "\n Invalit entry. Please enter a number greater than 0 and " << WEIGHT_MAX << " or less.\n";
std::cin.clear(); // <--- Resets state bits.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. Clears the input buffer.
}
else if (package_weight < WEIGHT_MIN || package_weight > WEIGHT_MAX)
{
std::cout << "\n Invalid weight! Weight should be greater than 0 and " << WEIGHT_MAX << " or less.\n";
}
std::cout << "\n What is the weight (LBS) of the package? ";
std::cin >> package_weight;
}
if (package_weight > WEIGHT_MAX)
{
std::cout << "\n We're sorry, box is too heavy.\n"
<< std::endl;
}
else // <--- Never executed if the previous if statement is true.
{
std::cout << "\n What is the area of the package (L x H x D in inches)?";
std::cout << "\n Enter Length: ";
std::cin >> length;
std::cout << " Enter Width: ";
std::cin >> width;
std::cout << " Enter Depth: ";
std::cin >> depth;
big = length * width * depth;
if (big < BIG_MIN)
{
std::cout << "\n We're sorry, the area must be"
<< " more than 0 "
<< std::endl;
}
else if (big > BIG_MAX)
{
std::cout << "\n We're sorry, box is too large. The area must be"
<< "less than 1001 \n"
<< " You have entered a size of " << big << '\n'
<< std::endl;
//if (big > BIG_MAX && package_weight > WEIGHT_MAX)
//{
// std::cout << "\n We're sorry, box is too large and too heavy.\n" // <--- Never reached.
// << std::endl;
//}
}
else
{
if (package_weight <= CHARGE_WEIGHT)
total_charges = (big / DIVISOR) * MIN_CHARGE;
else if (package_weight > CHARGE_WEIGHT)
total_charges = (big / DIVISOR) * MAX_CHARGE;
std::cout << "\n\n Box is OK.\n Total charges with tax are $"
<< total_charges
<< "\n For a area of "
<< big
<< " inches \n and a total weight of "
<< package_weight
<< " LBS. \n"
<< std::endl;
}
}
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue :";
std::cin.get();
return 0;
}
|
At the top I added the header file "<limits>" for the
1 2 3 4
|
std::cin.ignore(...)[/ code] I used.
Inside "main":
I changed and added to the constant variables.In your original code[code]const int WEIGHT_MIN = 0.00,
|
defining the variable as an "int" and then initializing it with a floating point number may work, but is not the proper way.The "0.0", as it should be written is considered a "double" and when stored the ".0" is dropped an only the whole number is stored.This is not a big problem, beyond the compiler warning, until there is something to the right of the decimal point and then you would have data loss.
For the way the program is written these constant variables work better as "double"s not "int"s.And these days "double" is the preferred floating point type and even then it can cause problems with calculations as it does not store some decimal numbers correctly, e.g., "0.03" may be stored as "0.029999999999999999". This is not a problem when printing only two decimal places as it rounds up properly, but can affect a calculation.Just so you know.
The other thing is that with "0.00" or "50.00" only one zero is needed.the extra zero does not help or hinder.
The
const double TAX_RATE{ 0.05 };
was a thought I had, but have not done anything with yet, but it does show storing a smaller number.
The next group of variables are initialized to zero with the empty{}. This may or may not be needed, but at least I know they do not contain a garbage value.
Line 29 as the comment says only needs done once in the program and will affect all "cout" statements the print a floating point number until the program ends or something is changed.I believe you understand the "std::fixed" part.The "std::showpoint" means that if the number is "50.0" show the decimal point and the zero.Otherwise it would drop the ".0" and only show the whole number.This is useful when lining up the decimal points of the output.
Using formatted input as with
std::cin >> package_weight;
the "cin" is expecting a number to be entered and anything that is not a number will cause "cin" to fail and become unusable the rest of the program unless corrected.
The while loop may be ahead of where you are at, but it is a good time to learn about it.
The while condition is first checking if the "cin" stream is in a good state, i.e., if the good bit is 1, if this has changed to(0)zero then you enter the while loop to deal with it.The second part is checking if "package_weight" is in the proper range.
The "if/else if" allows it to print one error message and not both.Using the "if/else if" combination allows for only need one prompt and "cin" to get a new number.When you have a proper number in the proper range the while loop is finished.
The while loop allows you to correct any problem instead of ending the program and having to start over.Once you get use to it you will find it better than what you start with in your program.
End part one.
Edit: Tags did not come out right.