-1

I'm a beginner at using visual basic and I'm having trouble displaying my years correctly in my application. My application should display Rates: 3% - 7%, then under each rate Years should display: 1-5 and Balance should display a total amount per year. What I'm getting is 3% rate, years 1-5, and balance for each year. 4% rate is getting year 6, instead of 1-5 and balance for year six. 5% Rate is displaying year 7 and balance for that year, and so on. FYI: I need to keep the For...Next statement and incorporate a Do...Loop statement for "Years."

I inserted pics for a better understanding of what I'm trying to explain. I'd appreciate any help.

Correct Way Incorrect Way

Here is the code:

    Dim dblDeposit As Double
    Dim dblBalance As Double
    Dim intYear As Integer = 1

    Double.TryParse(txtDeposit.Text, dblDeposit)

    txtBalance.Text = "Rate" & ControlChars.Tab &
        "Year" & ControlChars.Tab & "Balance" &
        ControlChars.NewLine

    ' Calculate and display account balances. 
    For dblRate As Double = 0.03 To 0.07 Step 0.01
        txtBalance.Text = txtBalance.Text &
            dblRate.ToString("P0") & ControlChars.NewLine

        Do
            dblBalance = dblDeposit * (1 + dblRate) ^ intYear
            txtBalance.Text = txtBalance.Text &
                ControlChars.Tab & intYear.ToString &
                ControlChars.Tab & dblBalance.ToString("C2") &
                ControlChars.NewLine
            intYear = intYear + 1
        Loop While intYear < 6

    Next dblRate

1 Answers1

1

You need to reset intYear before each iteration of the Do loop.

    Dim dblDeposit As Double
    Dim dblBalance As Double
    Dim intYear As Integer = 1

    Double.TryParse(txtDeposit.Text, dblDeposit)

    txtBalance.Text = "Rate" & ControlChars.Tab &
        "Year" & ControlChars.Tab & "Balance" &
        ControlChars.NewLine

    ' Calculate and display account balances. 
    For dblRate As Double = 0.03 To 0.07 Step 0.01
        txtBalance.Text = txtBalance.Text &
            dblRate.ToString("P0") & ControlChars.NewLine

        intYear = 1 ' Reset intYear here before each loop

        Do
            dblBalance = dblDeposit * (1 + dblRate) ^ intYear
            txtBalance.Text = txtBalance.Text &
                ControlChars.Tab & intYear.ToString &
                ControlChars.Tab & dblBalance.ToString("C2") &
                ControlChars.NewLine
            intYear = intYear + 1
        Loop While intYear < 6

    Next dblRate

Some other recommendations:

  • String concatenation in a loop is inefficient because of how String works in .NET (String is immutable, so every operation that changes a string causes a reallocation and a copy, this is a somewhat expensive operation), so I recommend you use a StringBuilder and only assign txtBalance.Text at the very end.
  • You need to abort if the user enters invalid input. Also use the built-in currency parser (NumberStyles.Currency)
  • Always use Decimal or integer pennies/cents when working with money. Never use IEEE-754 floating-point types (Single and Double) to represent money because they are imprecise.
  • Avoid using Hungarian Notation (prefixing variable names with an abbreviation of their type).
    Dim depositAmount As Decimal
    If Not Decimal.TryParse( txtDeposit.Text, NumberStyles.Currency, CultureInfo.CurrentCulture, depositAmount ) Then
        MsgBox( "Invalid input" ).
        Exit Sub
    End If
    Dim sb As New StringBuilder()
    sb.Append( "Rate    Year    Balance" ).AppendLine() ' Tab characters are embedded in the string literal.
    ' Calculate and display account balances. 
    For rate As Double = 0.03 To 0.07 Step 0.01
        sb.AppendFormat( "  {0:P0}", rate ).AppendLine();
        Dim year As Integer = 1 ' Reset intYear here before each loop
        Do
            Dim balance As Decimal = depositAmount * ( 1 + rate ) ^ year ' I think you should add extra parenthesis to make it clear which value the `^ year` is being applied to.
            sb.AppendFormat( "  {0:D}   {1:C2}", year, balance ).ToString()
            year = year + 1
        Loop While year < 6
    Next rate

    txtBalance.Text = sb.ToString()
Dai
  • 141,631
  • 28
  • 261
  • 374