# Need serious help with VB2010 assignment

Discussion in 'Programming & Webmastering' started by Killer_Rubber_Ducky, Jul 12, 2013.

1. ### Killer_Rubber_Ducky

Joined:
Oct 20, 2009
Messages:
2,832 (1.02/day)
345
Hey guys,

I have this VB2010 assignment I have to do by sunday.

I need help writing a Visual Basic program that calculates the area and circumference of a hot tub once I enter the inches and feet.

The professor left us with this clue:
and

We have not covered this information yet so I am stumped. I have no clue where to start.

The instructions for the program state that the application computes the area and circumference of a hot tub by entering the diameter in feet and inches. The area and circumference should go out to 2 decimal places. You should be able to clear the entry, enter a new diameter and exit the form.

I have been trying to do this assignment all week. I am not a programmer. My ASS degree requires I take a programming class.

This is what I have so far. I taught myself Qbasic back in the day but this is definately not QB any more.

2. ### FordGT90Concept"I go fast!1!11!1!"

Joined:
Oct 13, 2008
Messages:
19,876 (6.32/day)
9,141
Location:
IA, USA
First, your use of Const is going to create serious problems. Constants (= Const) which means the value is set at runtime and never changed (it is read only). Change those to Dim (= dimension) and that should fix that problem.

Second, you have a logical error here with your radius and diameter variables. You're treating them as functions and they simply aren't. Either you can move those into functions so they return the appropriate value or, better, you should put all the calculating in a subroutine tied to either a button or radio (in the case of inches or feet).

Third, "decimal places" means you're going to have to use Float, Double, or Decimial datatypes. You may want to look up the differences between those three but .NET is mostly developed for Double and Decimal but Decimal is intended for currency (only two decimal places) so I could recommend using Double. You should never round in algebra until the end.

Fourth, I would recommend focusing on the area and circumference first. Tackle that problem first and deal with inches/feet later. Conversion from feet to inches and inches to feet is fairly straight forward by comparison.

Fifth, I think I'll give you the basic area and circumference formulas to work off of:
Code:
```' Make sure diameter is not equal to 0 before running this
Dim area As Double = Math.PI * Math.Pow(diameter / 2, 2)
Dim circumference As Double = Math.PI * diameter```
Sixth, use Math.Round(variable, 2) to round to two places before converting the Doubles to text for displaying.

Last edited: Jul 12, 2013
Killer_Rubber_Ducky says thanks.

Joined:
Feb 8, 2012
Messages:
2,495 (1.29/day)
1,777
Location:
Zagreb, Croatia
Declare your variables (not constants) here but do not calculate anything yet ... you don't have any input here yet, calculate it on click.

Calculate them here reading input from the text boxes on the form, and update the labels that should display the result.

Killer_Rubber_Ducky says thanks.
4. ### Killer_Rubber_Ducky

Joined:
Oct 20, 2009
Messages:
2,832 (1.02/day)
345
Ok, I just ran through it again and can get it to calculate but the figures are WAY off. This is using a somewhat similar tutorial in the book.

5. ### FordGT90Concept"I go fast!1!11!1!"

Joined:
Oct 13, 2008
Messages:
19,876 (6.32/day)
9,141
Location:
IA, USA
Can you zip up the project and attach it so we can run it? It's a lot easier than creating a form to complement your code.

Killer_Rubber_Ducky says thanks.
6. ### Killer_Rubber_Ducky

Joined:
Oct 20, 2009
Messages:
2,832 (1.02/day)
345
View attachment HotTub.zip

This assignment is confusing as hell. I am assuming the hot tub is circular not rectangular. Why must I have the input dimensions in Feet and Inches? ugh I hate this book

7. ### FordGT90Concept"I go fast!1!11!1!"

Joined:
Oct 13, 2008
Messages:
19,876 (6.32/day)
9,141
Location:
IA, USA
Considering they aren't giving you the formulas for a rectangle, it certainly seems that it is circular.

Input in feet and inches is indeed messy. Standard practice is usually to keep it in one or the other, not mix them. Most likely it is to make it more difficult.

Anyway, on to the source...

In Visual Studio, you can step through programs using breakpoints to check the values at each stage. It should help you isolate where the logical error(s) are at.

The first thing you need to do is convert the string in txtNumberofFeet.Text and txtNumberOfInches.Text to doubles. There's not much sense in copying their values to a string variable so I would recommend changing strNumberofFeet and strNumberofInches to double. The compiler will then tell you where you need to convert from string to double and you can do that via CDbl() or, the more universal route, Convert.ToDouble().

Second, on line 28 and 29, you are adding feet to inches without multiplying feet by 12 to get inches. Instead of making all the formulas more complicated by converting feet to inches in them, I would recommend taking the two doubles in the step above and convert them into a unifying inch value. Use that inch value in all your formulas.

Third, you are using the same math for radius and diameter on both formulas. One requires a diameter and the other requires a radius. You also need to specify on the user interface what the user is to input: a diameter or a radius. I don't know if your instructions are explicit here but, anyway, it is a problem that needs to be resolved.

Fourth, again, avoid Decimal. Decimal is intended to hold the amount of US public debt (\$17 trillion), not small numbers with division and the like. Use Double (unless your instructions are explicit). You could get rounding problems with Decimal.

Fifth, I'm not sure why you are using 3.14 instead of Math.PI. If the instructions aren't explicit, Math.PI is better. Math.PI is a constant.

Sixth, it is a good idea to get in the habit of using Math.Pow instead of ^. In most languages, ^ is a bitwise XOR operator. It literally would make 1 to 0 and 0 to 1 (opposite from the binary perspective).

That should be enough feedback to get you on the right path.

Last edited: Jul 13, 2013
Killer_Rubber_Ducky says thanks.
8. ### Killer_Rubber_Ducky

Joined:
Oct 20, 2009
Messages:
2,832 (1.02/day)
345
Well on the Math.Pi part AFAIK we have not covered the Math function.

9. ### AquinusResident Wat-man

Joined:
Jan 28, 2012
Messages:
9,974 (5.13/day)
5,049
Location:
Concord, NH
Learning how to program is about learning the language and how to solve a problem, not necessarily all the objects and abilities of the language that enable you to solve almost any problem. If you're not sure about a class, you should google it and typically use the first link. If you're going to program in the real world, you'll be expected to figure this stuff out on your own, so I recommend making more of an effort to do research on how to do things because the information you seek does exist. I don't know everything about the languages I use. I know enough to write it and I know enough to use Google when I'm unsure how to do something or if something exists or not. Just an FYI.

By the way, Math.PI is public class constant (Math is the class, PI is the public constant). All you do is access it.

MxPhenom 216 says thanks.
10. ### Killer_Rubber_Ducky

Joined:
Oct 20, 2009
Messages:
2,832 (1.02/day)
345
well, Im taking the class since my major requires at least 1 programming class. I wanted to take C but all the C classes this summer did not "make" so I was stuck with VB. I self taught myself QBasic back in the day and could have easily made this program in QBasic minus the pretty program interface. ^_^ All of these classes and objects confuse the hell out of me. I am not planning on being a programmer. I can write Linux Shell scripts and old QBasic programs but this "new fangled" Visual BASIC is confusing and not "basic".

11. ### Killer_Rubber_Ducky

Joined:
Oct 20, 2009
Messages:
2,832 (1.02/day)
345
This is the final code I have decided to go with. Mainly since I am sick and tired of working on this program which seems like it should not have been assigned to INTRO to VB students in an Online class. I have gotten the calculations as close as possible. The Area seems right in Square inches while the Circumference is still way off it seems.

View attachment HotTub_Comings_Ian.zip

12. ### AquinusResident Wat-man

Joined:
Jan 28, 2012
Messages:
9,974 (5.13/day)
5,049
Location:
Concord, NH
That's because you're using Integers for math when they should all be floating point (or double) values. You're also converting the text to a double then storing it in a String so you're negating what you're trying to do in the first place.

Use the correct data type for what is being stored.

Code:
```Dim strRadiusNumberofFeet As String
The two variables should be doubles.

Code:
```Dim strDiameterFeet As String
Dim strDiameterInches As String```
Should be doubles.

Code:
`Dim intDiameterinches As Integer`
Should be a double, but shouldn't make a difference if you type-cast it to a double or if it's preceded by a double value.

Code:
```Dim AreaRadius As String
Dim CircumferenceDiameter As String
CircumferenceDiameter = Math.Round(Circumference, 2)```
Once again, use the right type for the data. This should be double. This shouldn't be textual data until you're ready to display it, you also can't round text.

This isn't a hard problem, you've just used the language incorrectly in many ways which is why it's behaving strangely.

Strings are text, do not treat them as numbers. If you're working with numbers, use numeric data types.

13. ### FordGT90Concept"I go fast!1!11!1!"

Joined:
Oct 13, 2008
Messages:
19,876 (6.32/day)
9,141
Location:
IA, USA
What Aquinus said. You should only be using doubles. The only time you should be using string is when you take a value from the GUI and when you write a value to the GUI. All the time in between, it should be double.

Also, I wouldn't submit what you have. That issue really needs to be fixed or you'll get a bad grade on it.

Killer_Rubber_Ducky says thanks.
14. ### MxPhenom 216Corsair Fanboy

Joined:
Aug 31, 2010
Messages:
12,003 (4.88/day)
3,669
Location:
Seattle, WA
That's some useful information. Ill be taking a Computer Science class coming this fall for my Computer Engineering major.

15. ### Killer_Rubber_Ducky

Joined:
Oct 20, 2009
Messages:
2,832 (1.02/day)
345
Please correct this section since it makes sense to me but will not compute correctly.

16. ### FordGT90Concept"I go fast!1!11!1!"

Joined:
Oct 13, 2008
Messages:
19,876 (6.32/day)
9,141
Location:
IA, USA
I'd suggest changing Dim Circumference As Double = (2 * Math.PI * radius) on line 31 (approximately) to Dim Circumference As Double = Math.PI * Diameterinches. You already have the diameter so why not use it?

What about it doesn't compute correctly? I put in 5'8" and got 3631.68 square inches area and 213.63 inch circumference. Note that it is inches, not feet (the GUI I have from the ZIP says Feet). If you're supposed to output feet, you might want to look into dividing by 12 or something. Anyway, make sure the units on the GUI match what you are displaying. If the value is square inches/feet, make sure to put square inches/feet as well. Units are very important.

Also, I don't know if you change the GUI but if you haven't, make sure to specify that the input expected is the diameter.

Going by your latest upload, you have fields for radius and diameter but it only uses diameter. You should either delete the inputs for radius or synchronize them using the TextChanged event on the TextBoxes. What I would do is make all four textboxes point to a single TextChanged event. I'd then write a function that pulls feet out of inches (use divide by 12 to get the feet and modulus 12 to get the inches) that returns a Double(2) array or ByRef types to return the value. Cast the Sender into TextBox and get the .Name property. If .Name.StartsWith("txtRadius") convert the feet and inches into inches, multiply by two, and use the function described above to populate the diameter textboxes. If the .Name.StartsWith("txtDiameter") convert the feet and inches into inches, divide by two, and use the function described above to populate the diameter textboxes. Make sure all four fields are assumed to be 0 if blank or otherwise invalid.

Since that function is pretty complicated, I'll provide it for you:
Code:
```    ' if value = 68
Public Function [b]InchesToFeetInch[/b](ByVal value As Double) As Double()
Dim output(1) As Double
output(0) = Math.Floor(value / 12) ' Sets 5
output(1) = value Mod 12 ' Sets 8
Return output ' Return (0) = 5 and (1) = 8
End Function```
To use it:
Code:
```        Dim feetinch() As Double = [b]InchesToFeetInch[/b](inches) ' if inches = 68
MsgBox(feetinch(0) & "'" & feetinch(1) & Chr(34)) ' prompt will say 5'8"```
If you have any questions about the above code, ask. It uses an array which you probably haven't been exposed to yet. If you haven't, it is no doubt coming.

Last edited: Jul 14, 2013
Killer_Rubber_Ducky says thanks.
17. ### Killer_Rubber_Ducky

Joined:
Oct 20, 2009
Messages:
2,832 (1.02/day)
345
hmm. you are right. I have been using a calculator to check the math and i must have been doing the math wrong on the calculator. I was not dividing the result in half for the radius!!

18. ### FordGT90Concept"I go fast!1!11!1!"

Joined:
Oct 13, 2008
Messages:
19,876 (6.32/day)
9,141
Location:
IA, USA
I made a pretty big edit to my previous post. Check it.

19. ### Killer_Rubber_Ducky

Joined:
Oct 20, 2009
Messages:
2,832 (1.02/day)
345
I had edited the form to remove all except the Diameter fields.

So your initial response was correct.
i had made the mistake of not taking into account that the Diameter is double the radius so If I took the answer I got on the calculator and divided it in half I got my answer.\
Anyways. Thanks to all for the help. I have been stuck in QBasic mode.
I submitted it along with the Use Case document and Event planner documents.

20. ### Killer_Rubber_Ducky

Joined:
Oct 20, 2009
Messages:
2,832 (1.02/day)
345
Just got the grade on the assignment.

Thanks to you guys' help, I scored 100% and the teacher said that it was a tough assignment and that the I did an excellent job on it even though it was complicated for beginners. I really wish there was a way to insert QBASIC code directly into a VB2010 program. I could have coded this thing in QB ( the math part anyway) and tied it to the form opr something.

21. ### KreijSenior Monkey ModeratorStaff Member

Joined:
Feb 6, 2007
Messages:
13,817 (3.67/day)
5,628
Location:
Cheeseland (Wisconsin, USA)
techPowerUp! ... Helping people with their homework since 2004! lol

Good job, Ducky.

FordGT90Concept says thanks.
22. ### shovenose

Joined:
Jan 11, 2013
Messages:
832 (0.52/day)
144
Location:
California
Nice, glad you got it worked out.

23. ### Killer_Rubber_Ducky

Joined:
Oct 20, 2009
Messages:
2,832 (1.02/day)
345
I just wrote a program in QBasic 4.5 for the same project. It works flawlessly and took maybe 10 minutes to write.

I have included the Standalone EXE I compiled as well as the .BAS file
The .BAS file is encoded only for QBasic compilers to read as Such I have posted the code below.

View attachment HotTubCalcbyIComings.zip

Code:
```PRINT "Welcome to the Hot Tub Dimensions Calculator."
PRINT "With the Calculator, you will be able to find the Area"
PRINT "and Circumference of the Hot Tub so that you may make an"
PRINT "informed choice in your purchase. As they say: Measure twice"
PRINT "Cut once."
PRINT
PRINT
PRINT
SLEEP 5
25 PRINT "What are the dimensions in Feet and Inches of the Tub in question?"
DIM DiamFeet AS DOUBLE
DIM DiamInch AS DOUBLE
INPUT "Feet"; DiamFeet
INPUT "Inches"; DiamInch
DIM Diameter AS DOUBLE
LET Diameter = (DiamFeet * 12) + DiamInch
DIM Pi AS DOUBLE
LET Pi = 3.14159265359#
LET r = Diameter / 2
LET Area = Pi * r ^ 2
LET Circumference = Pi * Diameter
IF Diameter > 0 THEN
PRINT "The Area of the Tub is:"; Area; "in^2"
PRINT "The Circumference of the Tub is:"; Circumference; "inches"
SLEEP 10
ELSE PRINT "The diameter is invalid. It must be greater than 0."
SLEEP 2
GOTO 25
END IF
DIM CalcAns AS STRING
INPUT "Would you like to calculate for another tub"; CalcAns
IF CalcAns = "yes" THEN
GOTO 25
ELSE END
END IF
END
```

Joined:
Feb 8, 2012
Messages:
2,495 (1.29/day)
1,777
Location:
Zagreb, Croatia
Cool. This code with very minor modifications could run in VB2010 using console application project.

25. ### FordGT90Concept"I go fast!1!11!1!"

Joined:
Oct 13, 2008
Messages:
19,876 (6.32/day)