Reading external data into array of struct

Reply

Join Date: Mar 2008
Posts: 2
Reputation: brdoolit is an unknown quantity at this point 
Solved Threads: 0
brdoolit brdoolit is offline Offline
Newbie Poster

Reading external data into array of struct

 
0
  #1
Mar 15th, 2008
I'm stuck on my homework trying to deal with arrays of structs. The prof is terrible and just reads off slides from another prof and talks about his weekend. Heres what we have to do:

You have been hired as a programmer for Super Sales Company. You are being asked to write a sales report program. You should implement this program as an array of structures (set MAXARRAY = 39). There will not be 40 items, so you must keep track of how many items you have read in. The format for each line in the file commiss.dat contains an employee’s first name, last name, employee id number and the amount of sales for each weekday. This file is on VSpace in the resources. The following is a sample of the input file:

John Fredricks 102 70.00 90.00 104.00 305.00 408.00
Jane Williams 104 89.00 105.00 70.00 400.00 207.00
Scott Howard 103 100.00 100.00 100.00 100.00 100.00

You should create a report which prints out the employee’s name, id number, 5 daily sales, total sales and average sales. Here is a sample of the report you should create:

Super Sales Associate
Weekly Sales Report
Name ID Mon Tue Wed Thu Fri Total Average

John Fredricks 102 70.00 90.00 104.00 305.00 408.00 977.00 195.40
Jane Williams 104 89.00 105.00 70.00 400.00 207.00 871.00 174.20
Scott Howard 103 100.00 100.00 100.00 100.00 100.00 500.00 100.00
Fred Wilkes 105 189.00 207.00 306.00 99.00 203.00 1004.00 200.80


You may find it easier to create a location for the total and average columns within the struct definition. I know that there are other ways to do this program, but you must use an array of structs since your next assignment will be a modification of this assignment.

My problem is that I don't know how to make it so I can read string by string. I only know how to read by lines. So my codes something like:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Structure EmployeeInfo
  2. Dim FirstName As String
  3. Dim LastName As String
  4. Dim ID As String
  5. Dim Mon As Double
  6. Dim Tue As Double
  7. Dim Wed As Double
  8. Dim Thu As Double
  9. Dim Fri As Double
  10. End Structure
  11.  
  12. Dim Data As EmployeeInfo
  13. Dim sr As IO.StreamReader = IO.File.OpenText("commiss.txt")

and then all I know how to do is Data.FristName = sr.ReadLine which stores the first line of data into Data.FirstName ....which I only want the first name.

So i'm wondering if theres something along the lines of Data.Firstname, Data.SecondName (etc) = sr.ReadLine? Something along the lines of the equivialnt to C++ like

infile >> Data.FirstName >> Data.SecondName >> Data.ID etc.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 2
Reputation: brdoolit is an unknown quantity at this point 
Solved Threads: 0
brdoolit brdoolit is offline Offline
Newbie Poster

Figured it out except now I'm having trouble with Split

 
0
  #2
Mar 16th, 2008
So after some more research I've managed to find the Split Function.  However I'm getting an error in the red:


 Structure EmployeeInfo
        Dim FirstName() As String
        Dim LastName() As String
        Dim ID() As String
        Dim Mon() As Double
        Dim Tue() As Double
        Dim Wed() As Double
        Dim Thu() As Double
        Dim Fri() As Double
    End Structure
    Dim MAX_SIZE As Integer = 39
    Dim Data(MAX_SIZE) As EmployeeInfo
    Dim line As String
    Dim total As Double
    Dim fmtStr As String = "{0,-10}{1,10}{2,8}{3,8}{4,8}{5,8}{6,8}{7,8}{8,10}"

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim i As Integer = -1
        Dim sr As IO.StreamReader = IO.File.OpenText("commiss.txt")
        PrintHeader()

        Do While (sr.Peek <> -1)
            i += 1
            line = (sr.ReadLine)
            Data(i).FirstName = line.Split(" "c)
            Data(i).ID = line.Split(" "c)
            Data(i).Mon = line.Split(" "c)
            Data(i).Tue = line.Split(" "c)
            Data(i).Wed = line.Split(" "c)
            Data(i).Thu = line.Split(" "c)
            Data(i).Fri = line.Split(" "c)

            total = Data(i).Mon + Data(i).Tue + Data(i).Wed + Data(i).Thu + Data(i).Fri

            lstDisplay.Items.Add(line)
        Loop
        
        'lstDisplay.Items.Add(Data.Tue)


    End Sub
    Sub PrintHeader()
        With lstDisplay.Items
            .Clear()
            .Add("                                     Super Sales Associate")
            .Add("                                      Weekly Sales Report")
            .Add("")
            .Add(String.Format(fmtStr, "Name", "ID", "Mon", "Tue", "Wed", "Thu", "Fri", "Total", "Average"))
            .Add(String.Format(fmtStr, "----", "--", "---", "---", "---", "---", "---", "-----", "-------"))
        End With
    End Sub
End Class

I've tried changing the structs to all string and trying to convert it to a double
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1.  
  2. Structure EmployeeInfo
  3. Dim FirstName() As String
  4. Dim LastName() As String
  5. Dim ID() As String
  6. Dim Mon() As String
  7. Dim Tue() As String
  8. Dim Wed() As String
  9. Dim Thu() As String
  10. Dim Fri() As String
  11. End Structure
  12. ...
  13. ...
  14. ...
  15. ...
  16. ...
  17. Data(i).Mon = line.Split(" "c)
  18. CDbl(Data(i).Mon
  19. etc...
but it seems like no matter what I try, I keep getting the error 'Double cannot be derived from String'

Any hints or help?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC