Results 1 to 8 of 8

Thread: Calories Counter

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Calories Counter

    I kinda Suck big time at Program. I'm a more hands on,

    but anyway i'm trying to get a simple Program up and running..

    Its a Calorie Counter.
    What i have is 2 Input text boxes 2 Labels and one label to Display the message and the % of Fat

    and 3 Buttons Go, Clear, Exit.

    at the Moment i have;

    Code:
            Dim Textbox1 As String = ""
            Dim TextBox2 As String = ""
            Dim lblDisplay As String = ""
            Dim Temp As String
            Dim Label3 As Integer
            Const dblCalories = 9
            Const dblfat = 1
    
            Do
                Textbox1 = (" ")
    
                If IsNumeric(Temp) = False Then
                    MessageBox.Show("Please enter a number!")
                End If
            Loop While IsNumeric(Temp) = False
      
            If dblfat < 30 Then
                Label3.caption("Health Verdict Eat Away, this is a low fat healthy option")
    
            End If
            If dblfat > 30 OR <70  Then 
    
                Label3.caption("Alright, it won’t kill you but you could choose a lower fat alternative!")
            End If
    
            If dblfat > 70 Then
                Label3.caption("Are you trying to kill yourself? This food is very fattening")
    
            End If
    End Sub

    The "If dblfat > 30 OR <70 Then" for some reason is coming up in Red the < 70 Then part that is.

    my maths are something like
    Percentage of calories from fat = Calories from fat/Total Calories

    What code do i need to get the LblDisplay show The Message of what ever the out come of dblfat is?

    Am i declaring the Text boxes rightly?

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Calories Counter

    You want to check if dblfat is greater than 30 and less than 70

    Code:
    If dblfat > 30 AndAlso dblfat < 70

  3. #3
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Calories Counter

    there's a lot wrong here and shows a VERY beginner level understanding of programming.

    You have controls, which are on your form. (Labels, Textboxes, Buttons, etc)
    These do not need to be declared in your code. The kind of declarations that you've made are used for variables and values.

    You've declared 2 constants (which are usually all caps)

    vb.net Code:
    1. Const dblfat = 1

    and you later go on to use them in a (presumably) dynamic condition statement:

    vb.net Code:
    1. If dblfat < 30 Then

    As a constant value, these will never change. Therefore, your condition statement should always yield the same result, in this case only the first "if" statement will ever be true because the value of dblfat never changes.

    In your do/while loop:

    vb.net Code:
    1. Do
    2.             Textbox1 = (" ")
    3.  
    4.             If IsNumeric(Temp) = False Then
    5.                 MessageBox.Show("Please enter a number!")
    6.             End If
    7.         Loop While IsNumeric(Temp) = False

    I don't see where you ever give the user the chance to change the value of "Temp", thereby resulting in an infinite loop.

    You've declared your Label3 as an Integer?? I don't even know how this could make sense, since you'll be using this label (and all labels for that matter) to display text (String) and at that, you don't declare controls, only their properties.

    vb.net Code:
    1. Label3.Text = "Health Verdict Eat Away, this is a low fat healthy option"

    I'm not even sure what information you're expecting from the user? Could you be more specific with what information the user is supposed to provide?

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Re: Calories Counter

    Ok, the user will enter a number in the text box 2 if the user enters 10 into the text box. 90 should come up on text box 1 and label 3 should show a message,

    that is all the program needs to do if I enter the number 1 into the text box then. The number 9 should come up on text box 1.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Re: Calories Counter

    vb Code:
    1. Dim intCal As Integer
    2.         Dim intgram As Integer
    3.         Dim intlabel3 As Integer
    4.         Dim dblfat As Double =
    5.         Dim dblCalories As Double =
    6.  
    7.         Const dblCalories = 9
    8.         Const dblfat = 1
    9.  
    10.  
    11.         intCal = CInt(txtCal.Text)
    12.         intgram = CInt(txtgram.Text)
    13.  
    14.         dblfat = dblfat * 9 + dblCalories
    15.  
    16.  
    17.         intlabel3 = CStr(intlabel3)
    18.  
    19.  
    20.  
    21.         If intCal < 30 Then
    22.             label3("Health Verdict Eat Away, this is a low fat healthy option")
    23.             Return
    24.         End If
    25.         IF intCal> 30 OR <70  Then
    26.             Return
    27.             label3("Alright, it won’t kill you but you could choose a lower fat alternative!")
    28.             Return
    29.         End If
    30.         If intCal > 70 Then
    31.             label3("Are you trying to kill yourself? This food is very fattening")
    32.         End If
    33.  
    34.  
    35.     End Sub

    Can some one tell me what im doing wrong and how do i Correct it.

  6. #6
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Calories Counter

    what is this?: intlabel3 = CStr(intlabel3)
    why would a label be an integer? you dont declare controls (as said before)

    also, what is this?: label3("Health Verdict Eat Away, this is a low fat healthy option")
    labels have a "Text" property, as I showed in a previous post. You need to set this text property equal to the text you would like to display:
    label3.Text = "Health Verdict Eat Away, this is a low fat healthy option"

    on this line: dblfat = dblfat * 9 + dblCalories
    you are basically saying: dblfat = 1 * 9 + 9
    so dblfat will ALWAYS be 18. ALWAYS! because you have them defined as constants

    in your condition statements, what happens if intCal is 30, what if it's 70? which condition gets met?

    also, If statements will all execute regardless of whether or not the previous If statement was true or false. So each time, ALL 3 of your If statements will be run, when really you just want to run until you find a condition that gets met. So basically, you want to run an If/Else structure.

    vb Code:
    1. If intCal <= 30 Then
    2.             label3.Text = "Health Verdict Eat Away, this is a low fat healthy option"
    3.         ElseIf intCal> 30 OR <70 Then
    4.             label3.Text = "Alright, it won’t kill you but you could choose a lower fat alternative!"
    5.         ElseIf intCal >= 70 Then
    6.             label3.Text = "Are you trying to kill yourself? This food is very fattening"
    7.         End If

    EDIT: can you post a picture of your form, as I'm still confused on what actual controls you have and what they are named

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Re: Calories Counter

    Ok, i add a Screen Shot of the Form.

    "On this line: dblfat = dblfat * 9 + dblCalories
    you are basically saying: dblfat = 1 * 9 + 9
    so dblfat will ALWAYS be 18. ALWAYS! because you have them defined as constants"

    I'm trying to get it that if a user types in 10 into the Fat (gram) text box 90 should come up in Calories text box and since its over 70 Calories "Are you trying to kill yourself? This food is very fattening" Should pop up on Label3
    Attached Images Attached Images   
    Last edited by jackey; Nov 28th, 2009 at 03:41 PM.

  8. #8
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Calories Counter

    ok.

    i used two textboxes (caloriesTextbox and fatgramsTextbox), 3 buttons (exitButton, clearButton, and calculateButton)

    I use the TextChanged event of the fatgramsTextbox to trigger the calculation of the calories

    I use the calculateButton's Click event to trigger the calculation of the "verdict" (outcome of the calculation)

    below code should work:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub fatgramsTextbox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles fatgramsTextbox.TextChanged
    4.         caloriesTextbox.Text = CInt(fatgramsTextbox.Text) * 9
    5.     End Sub
    6.  
    7.     Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
    8.         If CInt(caloriesTextbox.Text) <= 30 Then
    9.             verdictLabel.Text = "Verdict: Health Verdict Eat Away, this is a low fat healthy option"
    10.         ElseIf CInt(caloriesTextbox.Text) > 30 OR <70 Then
    11.             verdictLabel.Text = "Verdict: Alright, it won’t kill you but you could choose a lower fat alternative!"
    12.         ElseIf CInt(caloriesTextbox.Text) >= 70 Then
    13.             verdictLabel.Text = "Verdict: Are you trying to kill yourself? This food is very fattening"
    14.         End If
    15.     End Sub
    16.  
    17.     Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
    18.         Me.Close()
    19.     End Sub
    20.  
    21.     Private Sub clearButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clearButton.Click
    22.         caloriesTextbox.Text = ""
    23.         fatgramsTextbox.Text = ""
    24.         verdictLabel.Text = "Verdict: "
    25.     End Sub
    26. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width