4 Replies - 507 Views - Last Post: 30 April 2013 - 12:28 PM Rate Topic: -----

#1 Crovox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
            if (dateOfBirth.AddYears(age) > now)
                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: ");
            string firstName = Console.ReadLine();
            Console.Write("Last name: ");
            string lastName = Console.ReadLine();
            Console.Write("Gender: ");
            string gender = Console.ReadLine();
            Console.Write("Birthdate (MM-DD-YYYY): ");
            DateTime dateOfBirth = Convert.ToDateTime(Console.ReadLine());
            Console.Write("Height (in inches): ");
            int height = Convert.ToInt32(Console.ReadLine());
            Console.Write("Weight (in pounds): ");
            int weight = Convert.ToInt32(Console.ReadLine());

            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

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5572
  • View blog
  • Posts: 11,913
  • 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.


TOP most asked:
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



Was This Post Helpful? 0
  • +
  • -

#4 Crovox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Crovox  Icon User is offline

  • New D.I.C Head

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

Re: Constructor problem - can't divide by zero

Posted 30 April 2013 - 12:28 PM

View PostAdamSpeight2008, 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. :smile2:/>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1