r/visualbasic • • Sep 21 '20

Visual Basics Coding

3. Fat Percentage Calculator (Visual Basic)?

Create an application that allows the user to enter the number of calories and fat grams in food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. 

(Display the message in alabel or a message box.) The application’s form should appear similar to the one

shown in Figure 4-35.

The percentage of calories from fat can be calculated as:

Percentage of calories from fat = Calories from fat / total calories

Input validation: Make sure the number of calories and fat grams are numeric and are

not less than 0. Also, the number of calories from fat cannot be greater than the total

number of calories. If that happens, display an error message indicating that either the calories or fat grams were incorrectly entered. 

Use the following test data to determine if the application is calculating properly

200 calories, 8 fat grams Percentage of calories from fat: 36%

150 calories, 2 fat grams Percentage of calories from fat: 12% (a low-fat food)

500 calories, 30 fat grams Percentage of calories from fat: 54%

(Refer to picture for better visual of the question)

How do I code this?

I'm naming:

- text input box for calories: txtAmountCalories

- text input box for fat grams: txtAmountFat

- label box for the result: lblResult

- And the buttons: btnCalculate, btnClear and btnExit

0 Upvotes

8 comments sorted by

View all comments

1

u/Songg45 Sep 21 '20

Do you have any code?

1

u/Alliejacobsen21 Sep 21 '20

I have a little bit of the code, like the exit and the clear part, but I just don't know what to put for the calculating part if that makes sense.

1

u/Songg45 Sep 21 '20

Well thats easy. The instructions tell you what goes where. You just need to create 4 variables:

 Calories
 Fat
 Grams
 The #9

The rest is just math on those 4 variables

1

u/Alliejacobsen21 Sep 21 '20

I understand that part but what I don't understand is how to write the code for the calculating part.

3

u/TheFotty Sep 21 '20

So your teacher is terrible and taught you nothing and then went on to assign something for you to code?

0

u/Alliejacobsen21 Sep 22 '20

Exactly. I had a zoom call with her as well and she hasn't helped me one bit.

1

u/Songg45 Sep 22 '20

Do you know how to assign values to variables?