|
-
Nov 24th, 2009, 06:52 PM
#1
Thread Starter
New Member
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?
-
Nov 24th, 2009, 06:56 PM
#2
Re: Calories Counter
You want to check if dblfat is greater than 30 and less than 70
Code:
If dblfat > 30 AndAlso dblfat < 70
-
Nov 24th, 2009, 08:52 PM
#3
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)
and you later go on to use them in a (presumably) dynamic condition statement:
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:
Do
Textbox1 = (" ")
If IsNumeric(Temp) = False Then
MessageBox.Show("Please enter a number!")
End If
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:
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?
-
Nov 25th, 2009, 09:42 AM
#4
Thread Starter
New Member
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.
-
Nov 27th, 2009, 04:02 PM
#5
Thread Starter
New Member
Re: Calories Counter
vb Code:
Dim intCal As Integer Dim intgram As Integer Dim intlabel3 As Integer Dim dblfat As Double = Dim dblCalories As Double = Const dblCalories = 9 Const dblfat = 1 intCal = CInt(txtCal.Text) intgram = CInt(txtgram.Text) dblfat = dblfat * 9 + dblCalories intlabel3 = CStr(intlabel3) If intCal < 30 Then label3("Health Verdict Eat Away, this is a low fat healthy option") Return End If IF intCal> 30 OR <70 Then Return label3("Alright, it won’t kill you but you could choose a lower fat alternative!") Return End If If intCal > 70 Then label3("Are you trying to kill yourself? This food is very fattening") End If End Sub
Can some one tell me what im doing wrong and how do i Correct it.
-
Nov 27th, 2009, 04:17 PM
#6
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:
If intCal <= 30 Then
label3.Text = "Health Verdict Eat Away, this is a low fat healthy option"
ElseIf intCal> 30 OR <70 Then
label3.Text = "Alright, it won’t kill you but you could choose a lower fat alternative!"
ElseIf intCal >= 70 Then
label3.Text = "Are you trying to kill yourself? This food is very fattening"
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
-
Nov 28th, 2009, 03:35 PM
#7
Thread Starter
New Member
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
Last edited by jackey; Nov 28th, 2009 at 03:41 PM.
-
Nov 28th, 2009, 04:29 PM
#8
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:
Public Class Form1
Private Sub fatgramsTextbox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles fatgramsTextbox.TextChanged
caloriesTextbox.Text = CInt(fatgramsTextbox.Text) * 9
End Sub
Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
If CInt(caloriesTextbox.Text) <= 30 Then
verdictLabel.Text = "Verdict: Health Verdict Eat Away, this is a low fat healthy option"
ElseIf CInt(caloriesTextbox.Text) > 30 OR <70 Then
verdictLabel.Text = "Verdict: Alright, it won’t kill you but you could choose a lower fat alternative!"
ElseIf CInt(caloriesTextbox.Text) >= 70 Then
verdictLabel.Text = "Verdict: Are you trying to kill yourself? This food is very fattening"
End If
End Sub
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub clearButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clearButton.Click
caloriesTextbox.Text = ""
fatgramsTextbox.Text = ""
verdictLabel.Text = "Verdict: "
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|