• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Text Boxes and Int

In the following code, I get an error stating "      Error      1 Cannot implicitly convert type 'int' to 'string".  I'm new at this and have no clue what's going on LOL.  Any help is appreciated.  I am trying to get the int "BMI" to display in the text box txtBMI.  Also, am I correct in "Height = int.Parse(txtHeight.text)" etc?

private void cmdCalculate_Click(object sender, EventArgs e)
        {
            int X;
            int Y;
            int Height;
            int Weight;
            int Z = 703;
            int BMI;

            Height = int.Parse(txtHeight.Text);
            Weight = int.Parse(txtWeight.Text);
            X = Weight / Z;
            Y = X / Height;
            BMI = Y / Height;

            txtBMI.Text = BMI;


        }
0
fattumsdad
Asked:
fattumsdad
  • 6
  • 4
1 Solution
 
tinchosCommented:
You're getting the problem in the

            txtBMI.Text = BMI;
line

the problem is that the Text property is of type string and BMI is an integer

try using

            txtBMI.Text = BMI.ToString();

instead

Tincho
0
 
fattumsdadAuthor Commented:
tinchos,

That works, but it's giving me a value of "0"....  is my math wrong somewhere?
0
 
tinchosCommented:
If its showing a 0 then it's because that's the value of the variable

tell mewhat is it you're trying to get... so I can check it out

Tincho
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tinchosCommented:
Keep in mind that the result of the division is an integer, so it cannot have decimal values....

so, if the result is 0.7, the result is going to be 0

Tincho
0
 
fattumsdadAuthor Commented:
It's a body mass index (BMI) calculator.  I takes your weight (txtWeight), multiplies it by 703 (int Z), divides by your height in inches twice (txtHeight).  I hope that makes sense...  it's hard to put in writing :)

The result should be somewhere between 18.5 and 30, usually.  
0
 
tinchosCommented:
Just to a little math

if

            X = Weight / Z;
            Y = X / Height;
            BMI = Y / Height;

            txtBMI.Text = BMI;

then

BMI = Y / Height; and Y = X / Height;

so

BMI = X / ( Height * Height ) and  X = Weight / Z;

so

BMI = Weight / (Height * Height * 703)

Keep in mind that in order to get BMI > 0

Weight  > Height * Height * 703

Tincho
0
 
fattumsdadAuthor Commented:
But, technically...  that doesn't work....  say weight is 150....  and height is 64 inches....

150 * 703 = 105450
105450 / 64 = 1647.65625
1647.65625 / 64 = 25.74462890625

Going with Weight / (height * height * 703).....

150 / (64 * 64 * 703)....  
150 / (4096 * 703)...
150 / 2879488 = 19196.586666666666666666666666667

Two completely different numbers....  now, I'm not excellent at math, so I could have the order of operations completely twisted...  if so, please correct me.  
0
 
tinchosCommented:
If the first piece is what you're trying to get, then you must check that in your code you're making

            X = Weight / Z;                      // In your last post you're making     X = Weight * Z instead of it
            Y = X / Height;
            BMI = Y / Height;

Tincho
0
 
tinchosCommented:
If you want to accomplish

150 * 703 = 105450
105450 / 64 = 1647.65625
1647.65625 / 64 = 25.74462890625

then you should make

private void cmdCalculate_Click(object sender, EventArgs e)
        {
            int X;
            int Y;
            int Height;
            int Weight;
            int Z = 703;
            int BMI;

            Height = int.Parse(txtHeight.Text);
            Weight = int.Parse(txtWeight.Text);
            X = Weight * Z;
            Y = X / Height;
            BMI = Y / Height;

            txtBMI.Text = BMI;


        }

Hope this helps

Tincho
0
 
fattumsdadAuthor Commented:
Tincho,

Works great!  Thank you so much for the help, I really appreciate it!!!

Tony
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now