Results 1 to 3 of 3

Thread: Help please dat files and arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    2

    Question

    Hi, Im a student taking my first class in VB, I have a assigment that I am stuck on. They want us to create a application that uses a display button that reads a dat file and displays the information into label controls on the form. Displaying total males, females, and the average age of females and males. I can get the display button to display the total males and total females but I'm having problems figuring out the average age everything I try dosen't work. Here is the dat file and what I have. Any help would be great!

    Dat File
    "Mary","Female",21
    "Terri","Female",18
    "Jim","Male",39
    "Paula","Female",53
    "George","Male",45
    "Fred","Male",25


    Open "A:\1c2.dat" For Input As #1
    Do While Not EOF(1)
    Input #1, pstrName, pstrGender, psngAge
    Select Case pstrGender
    Case "Male"
    lblScore(0).Caption =Val(lblScore(0).Caption) + 1
    Case "Female"
    lblScore(2).Caption =Val(lblScore(2).Caption) + 1 End Select
    Loop
    Close #1
    End Sub






  2. #2
    Lively Member
    Join Date
    Aug 2000
    Location
    Australia
    Posts
    82
    To get the average of a group of numbers, you need to Divide the sum of the numbers by the count of actual numbers.

    In your Routine, Declare two integer variables, MaleCount and FemaleCount

    Code:
    Dim MaleSum as integer
    Dim MaleCount as integer
    Dim FemaleSum as integer
    Dim FemaleCount as integer
    'Your other declarations
    
    Open "A:\1c2.dat" For Input As #1 
    Do While Not EOF(1) 
       Input #1, pstrName, pstrGender, psngAge 
       Select Case pstrGender 
          Case "Male"
             MaleSum = MaleSum + psngAge 
             lblScore(0).Caption = Trim(Str(MaleSum))
             MaleCount = MaleCount + 1
          Case "Female" 
             FemaleSum = FemaleSum + psngAge
             lblScore(2).Caption = Trim(Str(FemaleSum))
             FemaleCount = FemaleCount + 1
       End Select 
    Loop 
    Close #1 
    lblMaleAv.caption = Trim(Str(MaleSum \ MaleCount)
    lblFemaleAv.caption = Trim(Str(FemaleSum \ FemaleCount)
    End Sub
    Hope this is of assistance
    Adrian

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    2

    Smile

    Thanks AdrianH I appreciate your help very much, Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width