Hi
trying changing this:
if (gender != toupper(m) && gender != toupper(f)) {
to:
1 2
|
char genderAsUpperCase = toupper(gender);
if (genderAsUpperCase != 'M' && genderAsUpperCase != 'F') {
|
The average should be of
double
type, you can delay the declaration of it until you have a value to assign to it:
double average_age = static_cast<double>total_age / iterations; // the cast promotes the calculation to be of type double
Don't forget to get rid of the first declaration of
average_age
at the top of the file.
Btw, please always use code tags
http://www.cplusplus.com/articles/z13hAqkS/
When using a
do
loop, put the
while
part on the same line as the closing brace, so it doesn't look like a
while
on it's own.
I am not a fan of
do
loops like this, just because it always executes at least once, IMO is not a reason on it's own to choose it over an ordinary
while
loop. One example of where it is needed or at least
convenient, is where something must inserted, even if it only loops once. An example for C programming (from Kernighan & Ritchie ) is the function
itoa
where at least 1 char must be inserted into a char array, even if the number of chars is 0.
All 3 types of loops (
while
,
for
,
do
) can be converted from one to another.
Try to avoid having global variables (those declared before main) - it's a good idea to restrict the scope of variables as much as feasible. For now you can put them all into the main function. It may not seems to make much difference now, but once you learn functions you will get to see how different it will be.
Good Luck !!