1 Replies - 4038 Views - Last Post: 26 October 2012 - 02:40 AM Rate Topic: -----

#1 apreludem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-October 12

having trouble with the code for a program project.

Posted 25 October 2012 - 10:23 PM

The question for the program is as follows:
Create an application that will predict the approximate size of a population of organisms. The user should select or enter the starting number of organisms in a combo box, enter the average daily population increase(as percentage) in a text box, and select or enter the number of days the organisms will be left to multiply in another combo box. For example, assume the user enters the following values:

Starting number of organisms: 2
Average daily increase: 30%
number of days to multiply: 10

So MY question is this: i created my text box, and my two combo boxes with some choices for the user OR the user can type their own into the combo box's text box. So i wrote the code ( or at least i thought i did ) so that the population could be calculated by multiplying the number of organisms by the average daily increase ( i left the percentage part out for now). What i get on my list box is ALL zeros...We are supposed to use some type of loop that uses the number of days to multiply as the counter. So if the user puts in 4, it will calculate it for day 1, then for day 2, then for day 3 and finally for day 4 and then it will stop. i used a for...next loop. Here is what code i have:

Public Class Form1

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        'closes the form.
        Me.Close()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'clears the list box.
        Output.Items.Clear()
        'clears the combo boxes
        cboOrganisms.SelectedIndex = -1
        cboOrganisms.Text = String.Empty
        cboDays.SelectedIndex = -1
        cboDays.Text = String.Empty
        'clears the text box
        intAverage.Clear()
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        'variables declared.
        Dim intAverage As Integer
        Dim intCount As Integer
        Dim intTotal As Integer
        'calculates the population
        For intCount = 1 To cboDays.Text
            intTotal = cboOrganisms.Text * intAverage
            Output.Items.Add(intTotal)
        Next
    End Sub
End Class 


Thanks for taking the time to read this long post.

Is This A Good Question/Topic? 0
  • +

Replies To: having trouble with the code for a program project.

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,797
  • Joined: 26-March 09

Re: having trouble with the code for a program project.

Posted 26 October 2012 - 02:40 AM

You're not populating your variable "intAverage", so it takes the value 0, anything multiplied by zero is zero.

Also, albeit this VB.Net code and in the wrong forum, try casting some of your datatypes to the correct type. VB is good at doing it itself, but it can lead to issues.

If you're multiplying integers, try to make sure your datatypes are both integers, don't use the Text property and allow VB to do the work.

Take a look at the CInt function (not sure of the VB.Net equivalent).

You should also look at validating the the user entries are indeed numeric.

Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1