Hi,
With bmi being initialized as zero, of course the program stops running when printed because anything divided by zero is illegal. |
Zero is not always a good choice to initialise with. Sometimes 1 is good, other times something obviously wrong is good.
Line 165:
=
is assignment, you want
==
for equality comparison.
The lines following that print out values if inches is zero, is that useful?
Line 175 does integer division, even though the result is supposed to be double. Use
static_cast
to make either weight or total_inches a double.
In
person::input()
you have some validation code at the end, would this not be better before assigning values to class members?
Consider writing a constructor that takes arguments which are the values of the class data members. Use an initialiser list to set the member variables. I changed the types to
std::size_t
- you could change the type of the class members too.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
person::person(const std::string& Name,
const std::size_t Feet,
const std::size_t Inches,
const std::size_t Weight,
const double Bmi = 1
)
: // colon introduces initialiser list
name (Name),
feet (Feet),
inches (Inches),
weight (Weight),
bmi (Bmi);
{
// either validate the data before calling the constructor
// or validate the data here, and throw exception if there are problems
// or set a global error variable, this is one valid use of a global
// invariants
// weight cannot be <= 0.
// feet must be >= 0
// 0 >= inches <= 12
// Put sensible limits on height & weight
}
|
I would choose different variable names, like
weightLoss
instead of
w
Good Luck !!
Edit: The
print
function should be marked
const
it doesn't affect the state of the class. Arguments to your functions should be
const
as well
161 162
|
void person::print () const
{
|
143 144 145
|
void person::lose (const int weightLoss) // consider making weightLoss std::size_t
{
if (w < 0)
|