| | |
Reading external data into array of struct
![]() |
•
•
Join Date: Mar 2008
Posts: 2
Reputation:
Solved Threads: 0
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:
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
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)
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 Data As EmployeeInfo 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
•
•
Join Date: Mar 2008
Posts: 2
Reputation:
Solved Threads: 0
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 ClassI'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)
Structure EmployeeInfo Dim FirstName() As String Dim LastName() As String Dim ID() As String Dim Mon() As String Dim Tue() As String Dim Wed() As String Dim Thu() As String Dim Fri() As String End Structure ... ... ... ... ... Data(i).Mon = line.Split(" "c) CDbl(Data(i).Mon etc...
Any hints or help?
![]() |
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Listview function
- Next Thread: How to open access file & pdf file using visual basic?
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





