# Constructor problem - can't divide by zero

Page 1 of 1

## 4 Replies - 1165 Views - Last Post: 30 April 2013 - 12:28 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=319950&amp;s=b599b478b503f60d5e055643f7b9c81e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Crovox

Reputation: 0
• Posts: 21
• Joined: 30-April 13

# Constructor problem - can't divide by zero

Posted 30 April 2013 - 11:28 AM

My program seems to work just fine until it calls the BMI method. For some reason it's reading the values of height and weight as 0. Even when I comment out the calling of the BMI method my other methods yield the wrong answers. I can't figure out what I'm doing wrong.

```namespace Health_Records
{
class HealthProfile
{
public string firstName { get; set; }
public string lastName { get; set; }
public string gender { get; set; }
public DateTime dateOfBirth { get; set; }
public int height { get; set; }
public int weight { get; set; }

public HealthProfile(string firstName, string lastName, string gender, DateTime dateOfBirth, int height, int weight)
{
firstName = this.firstName;
lastName = this.lastName;
gender = this.gender;
dateOfBirth = this.dateOfBirth;
height = this.height;
weight = this.weight;
}

public int CalculateAge(DateTime dateOfBirth)
{
DateTime now = DateTime.Today;
int age = now.Year - dateOfBirth.Year;
age--;
return age;
}

public int MaxHeartRate()
{
int maxHeartRate = 220 - this.CalculateAge(dateOfBirth);
return maxHeartRate;
}

public double MinTargetHeartRate()
{
double minTargetHeartRate = 0.5 * this.MaxHeartRate();
return minTargetHeartRate;
}

public double MaxTargetHeartRate()
{
double maxTargetHeartRate = 0.85 * this.MaxHeartRate();
return maxTargetHeartRate;
}

public string BMI()
{
double bodyMassIndex = ((weight * 703) / (height * height));
string value;

if (bodyMassIndex >= 30)
value = "Obese.";
else
if (bodyMassIndex >= 25)
value = "Overweight.";
else
if (bodyMassIndex >= 18.5)
value = "Normal.";
else
value = "Underweight.";

return bodyMassIndex + " - " + value;
}
}
}
```

```namespace Health_Records
{
class Program
{
static void Main(string[] args)
{
Console.Write("First name: ");
Console.Write("Last name: ");
Console.Write("Gender: ");
Console.Write("Birthdate (MM-DD-YYYY): ");
Console.Write("Height (in inches): ");
Console.Write("Weight (in pounds): ");

HealthProfile healthProfile = new HealthProfile(firstName, lastName, gender, dateOfBirth, height, weight);

Console.Clear();

Console.WriteLine("USER INFORMATION\n------------------------");
Console.WriteLine("Name: " + firstName + " " + lastName);
Console.WriteLine("Gender: " + gender);
Console.WriteLine("Date of birth: " + dateOfBirth.Day + "/" + dateOfBirth.Month + "/" + dateOfBirth.Year);
Console.WriteLine("Height: " + height + "\"");
Console.WriteLine("Weight: " + weight + " lbs.");
Console.WriteLine();
Console.WriteLine("HEALTH PROFILE\n------------------------");
Console.WriteLine("Age: " + healthProfile.CalculateAge(dateOfBirth));
Console.WriteLine("BMI: " + healthProfile.BMI());
Console.WriteLine("Maximum heart rate: " + healthProfile.MaxHeartRate());
Console.WriteLine("Target heart rate: " + healthProfile.MinTargetHeartRate() + "-" + healthProfile.MaxTargetHeartRate());
Console.WriteLine();
Console.WriteLine("BMI VALUES");
Console.WriteLine("-----------------------------------");
Console.WriteLine("| Underweight: less than 18.5     |");
Console.WriteLine("| Normal: between 18.5 and 24.9   |");
Console.WriteLine("| Overweight: between 25 and 29.9 |");
Console.WriteLine("| Obese: 30 or greater            |");
Console.WriteLine("-----------------------------------");
}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Constructor problem - can't divide by zero

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Constructor problem - can't divide by zero

Posted 30 April 2013 - 11:36 AM

Use the debugger and walk through your constructor.
What is actually happening?
Spoiler

### #3 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6497
• Posts: 14,339
• Joined: 02-June 10

## Re: Constructor problem - can't divide by zero

Posted 30 April 2013 - 11:43 AM

tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

FAQ (Frequently Asked Questions - Updated Apr 2013
Spoiler

### #4 Crovox

Reputation: 0
• Posts: 21
• Joined: 30-April 13

## Re: Constructor problem - can't divide by zero

Posted 30 April 2013 - 12:08 PM

Well my error occurs in this line of code...
```double bodyMassIndex = ((weight * 703) / (height * height));
```

But I'm not sure why variables height and weight are suddenly zero when the program seems to read them just fine before this method.

### #5 Crovox

Reputation: 0
• Posts: 21
• Joined: 30-April 13

## Re: Constructor problem - can't divide by zero

Posted 30 April 2013 - 12:28 PM

AdamSpeight2008, on 30 April 2013 - 11:36 AM, said:

Use the debugger and walk through your constructor.
What is actually happening?
Spoiler

Ah thank you. Problem solved. />