0

I've been working on this program for the past two hours and I'm completely stumped. In short, the user is to enter in a population in the first text box and a growth rate in the second text box. The program then calculates the population and difference in population for the next 75 years and displays it in a third text box. Here is a screenshot of what the program is supposed to look like:

enter image description here

This is what my code looks like so far:

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim num As Double = System.Convert.ToDouble(TextBox1.Text)
    Dim num1 As Double = System.Convert.ToDouble(TextBox2.Text)
    Dim num2 As Double = num1 / (100 + 1)
    TextBox3.Text = "Year      Population    Increase"
    Dim num3 As Integer = 1

    While num3 <= 75
        Dim num4 As Double = num
        num = num * num2
        Dim num5 As Double = num - num4
        Dim text As String = TextBox3.Text, "" & vbCrLf & "", num3.ToString(), "            ", num.ToString("F"), "      ", num5.ToString("F")
        TextBox3.Text = System.Convert.ToString(text)
        num3 = num3 + 1

    End While

End Sub

I can't seem to figure how I can combine the "Year Population Increase" with the calculations in my while loop. If somehow can point me in the right direction as to how I can accomplish this, that would be greatly appreciated.

Thanks in advance!

3
  • are you wanting to keep "Year Population Increase" at the top as a header? I dont understand "combine". Your loop could just be For n As Integer = 1 To 75 instead of a Do/While loop Commented Apr 20, 2014 at 22:27
  • Yeah I want to list the calculations for the next 75 years under "Year Population Increase". That's what I meant by combining. I'm at loss at how I can do that. Commented Apr 20, 2014 at 22:30
  • why not just make them labels above the textbox? Commented Apr 20, 2014 at 22:32

3 Answers 3

2

a more concise version:

Dim popBasis As Double = Convert.ToDouble(TextBox1.Text)
Dim growthRate As Double = Convert.ToDouble(TextBox2.Text)

growthRate = growthRate / 100
' cannot anchor "rows" in a ML TB, so make this some labels:
'TextBox3.Text = "Year      Population    Increase"

Dim popIncr as Double

For n As Integer = 1 to 75
    popIncr = popBasis * growthRate
    popBasis += popIncr

    TextBox3.Text &= n.ToString & vbTab & popBasis.Tostring & 
         vbTab & popIncr.ToString & Environment.NewLine

    ' or:
    TextBox3.Text &= String.Format({0} {1} {2} {3}, n.ToString, 
             popBasis.Tostring, popIncr.ToString, Environment.NewLine)

Next n

Using the String.Format approach, you could tinker with using PadLeft/PadRight to emulate columns. this only works with monospace fonts. yet another alternative would be to use a ListView which has actual column headers

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help so far guys! Keep in mind I'm using VB.net and not VB. I tried both suggestions with slightly altered code since I'm using VB.net and the third text box outputs wrong numbers and are squashed together. My calculation code is correct but I can't find out how to implement it in the third text box.
thats .NET code... the squashing comes from non monospace fonts - "1" takes less room than "0" or "8" (and "1" takes less room than "11"). for true column layout you need something like a ListView not a textbox.
Hmmm... the picture I linked above looks like a multi-line textbox with a vertical scrollbar. Is this not the case?
it is probably a listbox which does support columns but still no anchored headers
I don't think we've covered the function of the listbox yet in my class. Here are the instructions for this program: Write an app that accepts two inputs (see preview) and calculates city population growth each year for the next 75 years, using the simplifying assumption that the current growth rate will stay constant. Print the results in a textbox (multiple lines with scroll bar)
0

First suggestion, name your Variables in descriptive way. Second, declare the variable text outside the while loop.

Dim text as string = string.empty

Third suggestion, do something like this in your loop, but would me much better if you use StringBuilder to concatenate strings.

While num3 <= 75
        Dim num4 As Double = num
        num = num * num2
        Dim num5 As Double = num - num4
         text  =  text & num3.ToString() & vbtab & num.ToString("F") & vbtab & num5.ToString("F") & vbcrlf

        num3 = num3 + 1

    End While

Then assign the variable text in the textbox3 outside the loop.

textbox3.text = textbox3.text + vbcrlf + text

All in all, your code will resemble like this:

Dim num As Double = System.Convert.ToDouble(TextBox1.Text)
    Dim num1 As Double = System.Convert.ToDouble(TextBox2.Text)
    Dim num2 As Double = num1 / (100 + 1)
    TextBox3.Text = "Year      Population    Increase"
    Dim num3 As Integer = 1

Dim text as string = string.empty

While num3 <= 75
        Dim num4 As Double = num
        num = num * num2
        Dim num5 As Double = num - num4
          text  =  text & num3.ToString() & vbtab & num.ToString("F") & vbtab & num5.ToString("F") & vbcrlf

        num3 = num3 + 1

    End While

textbox3.text = textbox3.text + vbcrlf  + text

4 Comments

I just tried out your suggestion and this is what I got - i.imgur.com/wKF4aGI.jpg
Replace Dim text as string to Dim text as string = string.empty
It yields the same same result. I just double checked my calculations and it seems to make sense. I'm not sure why my output is so skewed.
Try the calculation as commented by Plotunix. Its how you compute growthRate. My suggestion merely where you declare your variables. Placing it in the right scope but not how you compute the growth rate. I think it would be better to let you solve the correct computation. As I see it, you are almost there. :)
0

maybe with vbTab

Dim text As String = "Year" & vbTab & "Population" & vbTab & "Increase" & vbCrLf

and

While num3 <= 75
    Dim num4 As Double = num
    num = num * num2
    Dim num5 As Double = num - num4
    text = text & num3.ToString() & vbTab & num.ToString("F") & vbTab & num5.ToString("F") & vbCrLf
    num3 = num3 + 1
End While
TextBox3.Text = text

TextBox3 being multiline

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.