4 Replies - 717 Views - Last Post: 01 October 2013 - 08:50 AM Rate Topic: -----

#1 swilc11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-October 13

Code debugging program not working (intro to VB)

Posted 01 October 2013 - 07:55 AM

Hi everyone. I have a homework assignment that for some reason despite my efforts I can't figure out. I am to make a fat percentage calculator using if then statements for some error codes if the user enters the wrong information. I haven't made it to the if/then statements yet I have just been working on the actual calculator portion. I have all of the design correct to my knowledge at least. My code is as follows:

 

Private Sub Button1_Click( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
        Dim intCalories As Integer
        Dim intFat As Integer
        Dim intPercent As Integer

        
        intCalories = CInt(txtCalories.Text)
        intFat = CInt(txtFat.Text)
    
        intPercent =   intFat * 9 / intCalories
            


        lblPercent.Text = intPercent.ToString("n2")
End Sub
End Class




The fat percentage will be shown in a label once it is calculated. The book tells us that calories from fat = fat grams * 9 and percentage of calories from fat = calories from fat / total calories. The user will enter the amount of calories and fat in the food. Is this a mathematical error that I am just not catching from looking at this for too long or am I making a code mistake.

Also - the percentage of fat should have a % sign in the label. I've tried the ("p") for the last line of code however learned that when you do that the program multiplies the numbers, which is not what I want, I just want the % symbol. I cannot find anything in my book about how to achieve this. I put a % sign in the label and made the visibility false but that didn't work.

I am very new to all of this and have been steadily getting the hang of it which is why this is frustrating because it should be somewhat simple!

Thank you so much!

Is This A Good Question/Topic? 0
  • +

Replies To: Code debugging program not working (intro to VB)

#2 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: Code debugging program not working (intro to VB)

Posted 01 October 2013 - 08:14 AM

this question is for vb.net. moderators pls. move it.
Was This Post Helpful? 0
  • +
  • -

#3 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: Code debugging program not working (intro to VB)

Posted 01 October 2013 - 08:27 AM

anyway. since you are dealing with possible decimal output in here.
try using double variable type instead of int.
and turn on option strict on in your class for you to avoid getting into headaches that you shouldn't need to feel in the first place.

to turn option strict
just put the this code at the above of your form class/Imports
Option Strict On



regarding the percentage symbol
just add this to your code
line # 14

lblPercent.Text = intPercent.ToString("n2") & "%"



hope it helped.

This post has been edited by jimzcoder: 01 October 2013 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 swilc11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-October 13

Re: Code debugging program not working (intro to VB)

Posted 01 October 2013 - 08:41 AM

Sorry if this appears twice, it didn't show up the first time I responded. Thank you very much I will add that to my code for the percentage. I appreciate the help. However my main issue is the equation. I think I have the equation correct but when I run the program it is not giving me the correct percentage. That is why I don't know if it's an equation issue or a coding issue because I am fairly positive there is nothing wrong with the equation, however, math has never been my strongest suit. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#5 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: Code debugging program not working (intro to VB)

Posted 01 October 2013 - 08:50 AM

have you already tried changing the variable types?
if you haven't yet. you can give it a shot.
most of the time complex problem can be solved by a simple solution.
It's from the fact that the definition of complex is just a series of simple objects/methods being combined together.
So for us to solve a complex problem. we just have to look at the light side, which is the simplicity behind it.

:punk:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1