Paradoxz1
Member
- Joined
- Feb 8, 2021
- Messages
- 14
- Programming Experience
- Beginner
Hi,
I have been trying for the past 6 days to get this BMI calculator working to my requirements but to no success. I am not even able to get a value out from it. I don't know where to start looking for the issue as I have been looking at this code for hours now and been looking at different guides and such but I am no closer to getting this working. This forum is my last resort as it has been with previous projects.
As I don't know where to issue lies ill post my code and some images in hopes that one of you can help me figure out where the issue lies. I don't want you to send me a working code as response but help me figure out where the issue is and what method I should use in order to solve it.
Form1.cs [Design]
I realise this is a big ask, but I felt I had exhausted all my other options as the material I have to look at for help IMO not very good and well structured and I have been trying to solve this with the help if that for the past 6 days to no avail.
Results when running program
No matter the input on Height or Weight the "Weight Category" is always 'Underweight' and "BMI" is always whatever those symbols are. Even though I have tried to use double.TryParse to make a messagebox popup when Height and Weight are invalid (non numbers) the results are the same for Metric and Imperial (Us) units.
To end this long post, id like to really thank anyone who has read trough all of this as for me atleast this is alot of code and I would be really greatful for any input on the matter, as I said this is my last resort and im not looking for handout merley help on where to even start as when I look at the code with my eyes I feel like it should work but then again im new to this and the results show that it is not working so ye...
Best regards
I have been trying for the past 6 days to get this BMI calculator working to my requirements but to no success. I am not even able to get a value out from it. I don't know where to start looking for the issue as I have been looking at this code for hours now and been looking at different guides and such but I am no closer to getting this working. This forum is my last resort as it has been with previous projects.
As I don't know where to issue lies ill post my code and some images in hopes that one of you can help me figure out where the issue lies. I don't want you to send me a working code as response but help me figure out where the issue is and what method I should use in order to solve it.
Form1.cs [Design]
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Assignment_3_New
{
public partial class Form1 : Form
{
private BMICalculator bmiCalc = new BMICalculator();
public Form1()
{
InitializeComponent();
InitializeGUI();
}
private void InitializeGUI()
{
this.Text = "The Body Mass Calculator by [Name]";
//input
radbtnMetric.Checked = true;
lblHeight.Text = "Height (cm)";
lblWeight.Text = "Weight (kg)";
//output
txtHeight.Text = string.Empty;
txtWeight.Text = string.Empty;
}
private void DisplayResults()
{
lblBMICalculated.Text = bmiCalc.CalculateBMI().ToString("f2");
lblWeightCatCalculated.Text = bmiCalc.BMIWeightCategory().ToString();
grpResult.Text = "Results for " + bmiCalc.GetName();
}
private void label1_Click(object sender, EventArgs e)
{
}
//********************************************* Read Weight *********************************************
#region Read Weight
private bool ReadWeight()
{
double outValue = 0;
bool ok = double.TryParse(txtWeight.Text, out outValue);
if (ok)
{
if (outValue > 0)
{
bmiCalc.SetWeight(outValue);
}
else
ok = false;
}
if (!ok)
MessageBox.Show("Invalid weight value!", "Error");
return ok;
}
#endregion
private void textBox3_TextChanged(object sender, EventArgs e) //Weight
{
}
private void textBox1_TextChanged(object sender, EventArgs e) //Name
{
bmiCalc.SetName(txtName.Text);
}
//********************************************* Read Height *********************************************
#region Read Height
private bool ReadHeight()
{
double outValue = 0;
bool ok = double.TryParse(txtHeight.Text, out outValue);
if (ok)
{
if (outValue > 0)
{
if (bmiCalc.GetUnit() == UnitTypes.American)
{
bmiCalc.SetHeight(outValue * 12.00);
}
else
{
bmiCalc.SetHeight(outValue / 100.0);
}
}
else
ok = false;
}
if (!ok)
MessageBox.Show("Invalid height value!", "Error");
return ok;
}
#endregion
private void textBox2_TextChanged(object sender, EventArgs e) //Height
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void lblWeightCat_Click(object sender, EventArgs e)
{
}
private void lblBMI_Click(object sender, EventArgs e)
{
}
private void grpResult_Enter(object sender, EventArgs e)
{
}
//********************************************* Read Input BMI *********************************************
#region Read Input BMI
/*private bool ReadInputBMI()
{
bool CalculateClicked = false;
btnCalculate.Click
}*/
#endregion
private void btnCalculate_Click(object sender, EventArgs e)
{
/*bool ok = ReadInputBMI();
if (ok)
{
DisplayResults();
}*/
DisplayResults();
}
private void grpUnit_Enter(object sender, EventArgs e)
{
}
private void radbtnUs_CheckedChanged(object sender, EventArgs e)
{
if (radbtnUs.Checked)
{
lblHeight.Text = "Height (inch)";
lblWeight.Text = "Weight (lbs)";
bmiCalc.SetUnit(UnitTypes.American);
}
}
private void radbtnMetric_CheckedChanged(object sender, EventArgs e)
{
if (radbtnMetric.Checked)
{
lblHeight.Text = "Height (cm)";
lblWeight.Text = "Weight (kg)";
bmiCalc.SetUnit(UnitTypes.Metric);
}
}
private void WeightCat_Click(object sender, EventArgs e)
{
}
private void lblBMICalculated_Click(object sender, EventArgs e)
{
}
}
}
BMICalculator.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_3_New
{
class BMICalculator
{
private string name = "No name";
private double height = 0;
private double weight = 0;
private UnitTypes unit;
// ********************************************* Getters *********************************************
#region Getters
public string GetName()
{
return name;
}
public double GetHeight()
{
return height;
}
public double GetWeight()
{
return weight;
}
public UnitTypes GetUnit()
{
return unit;
}
#endregion
// ********************************************* Setters *********************************************
#region Setters
public void SetName(string value)
{
if (!string.IsNullOrEmpty(value))
{
name = value;
}
}
public void SetHeight(double value)
{
if(value >= 0)
{
height = value;
}
}
public void SetWeight(double value)
{
if(value >= 0)
{
weight = value;
}
}
public void SetUnit(UnitTypes value)
{
unit = value;
}
#endregion
//********************************************* Calculate BMI *********************************************
#region Calculate BMI
public double CalculateBMI()
{
double bmi = 0.00;
if(unit == UnitTypes.American)
{
bmi = 703 * weight / (height * height);
}
else if(unit == UnitTypes.Metric)
{
bmi = weight / (height * height);
}
return bmi;
}
#endregion
//********************************************* BMI Weight Category *********************************************
#region BMI Weight Category
public string BMIWeightCategory()
{
double bmi = CalculateBMI();
string stringout = string.Empty;
if (bmi >= 30)
stringout = "Obesity";
else if (bmi < 30)
stringout = "Overweight";
else if (bmi < 25 && bmi >= 18.5)
stringout = "Normal Weight";
else
stringout = "Underweight";
return stringout;
}
#endregion
}
}
UnitTypes.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_3_New
{
enum UnitTypes
{
Metric,
American
}
}
I realise this is a big ask, but I felt I had exhausted all my other options as the material I have to look at for help IMO not very good and well structured and I have been trying to solve this with the help if that for the past 6 days to no avail.
Results when running program
No matter the input on Height or Weight the "Weight Category" is always 'Underweight' and "BMI" is always whatever those symbols are. Even though I have tried to use double.TryParse to make a messagebox popup when Height and Weight are invalid (non numbers) the results are the same for Metric and Imperial (Us) units.
To end this long post, id like to really thank anyone who has read trough all of this as for me atleast this is alot of code and I would be really greatful for any input on the matter, as I said this is my last resort and im not looking for handout merley help on where to even start as when I look at the code with my eyes I feel like it should work but then again im new to this and the results show that it is not working so ye...
Best regards