O.k. this is my first scripting class and I need someone's opinion on this code. I need to write a code for a modularized program that will:

1.) read each record from the inventory database

2.) print a price tag for each item in inventory

3.) count and print the total number of tags printed

4.)sum and print the total retail value of the inventory in the store

Here's the code I came up with. Please tell me if it is correct or not.

start
   perform housekeeping ('This will declare variables perform housekeeping )	(with read)
     while not eof
     perform mainLoop (‘This will read each record from the database)
perform calculateTotalretailvalue ( )
endwhile 
perform finishup ( )
stop

housekeeping ( )
       declare variables
           
              invRecord
                         char	invDescription
                         num	invItemnumber
                         num	invQuantity
                         num	invRetailprice
                         num	invTotalretailvalue
                         num	invTotaltags

             char mainHeading = "INVENTORY REPORT"	
             char columnHead1 =
               "ITEM	RETAIL PRICE	QUANTITY		DESCRIPTION"

             char columnHead2 =
               "TOTAL RETAIL VALUE		TOTAL TAGS	ITEM NUMBER"
             num totalRetailvalue
open files
       INVENTORY , Printer
print mainHeading
print columnHead1
print columnHead2
read invRecord
return

mainloop ( )
Read the first record
While not eof
       Print fldItemnumber , fldDescription , fldQuantityonhand , fldRetailPrice
       ItemCount = ItemCount +1
       Totalretailvalue = (fldQuantity * fldRetailprice )
endwhile
Read next record
Loop

Print “Retail price : “ & ItemCount
Print “Value of Inventory = “ & Totalretailvalue
return

calculateTotalretailvalue ( )
     Totalretailvalue = (fldQuantity * fldRetailprice )
return

printTotalretailvalue ( )
     print Totalretailvalue
return
finishup ( )
   close files
       INVENTORY , Printer
return

Dani AI

Generated

The posted outline from shows the right high-level steps, but it is pseudocode rather than valid Visual Basic and contains several logic errors (as pointed out). The common mistakes: totals are overwritten instead of accumulated, loop/read order is incorrect, totals/counters are not initialized, and there is no data validation or error handling. The print/close operations belong outside the record loop.

Recommended, minimal flow: open the data source, initialize totalTags = 0 and totalRetail = 0, loop over records with a proper reader (While reader.Read() or While Not sr.EndOfStream for files), for each record validate and parse fields, print the tag (use PrintDocument for Windows printing or buffer output for testing), increment the tag counter, add qty * price to the running total, then after the loop print the totals and close resources. Use Integer for counts and Decimal for money; never use Double for currency.

Example VB.NET pattern (DB read + accumulation):

Dim totalTags As Integer = 0
Dim totalRetail As Decimal = 0D

Using conn As New SqlConnection(connectionString)
    conn.Open()
    Using cmd As New SqlCommand("SELECT ItemNumber, Description, QuantityOnHand, RetailPrice FROM Inventory", conn)
        Using rdr As SqlDataReader = cmd.ExecuteReader()
            While rdr.Read()
                Dim itemNumber As Integer = If(rdr.IsDBNull(0), 0, rdr.GetInt32(0))
                Dim descr As String = If(rdr.IsDBNull(1), String.Empty, rdr.GetString(1))
                Dim qty As Integer = If(rdr.IsDBNull(2), 0, rdr.GetInt32(2))
                Dim price As Decimal = If(rdr.IsDBNull(3), 0D, rdr.GetDecimal(3))

                ' print tag here (PrintDocument or build output)
                totalTags += 1
                totalRetail += qty * price
            End While
        End Using
    End Using
End Using

' Example print: Console.WriteLine(totalRetail.ToString("C"))

Troubleshooting notes: check for DBNull and use TryParse where reading from text; wrap IO in Using and add Try/Catch for robust error handling; format currency with ToString("C"). For targeted code review, post syntactically correct VB code inside code tags with a sample record set (as advised).

Recommended Answers

All 2 Replies

Sorry,
But it is not VB code.
And please, use the [ CODE][/CODE ] tags.

Isn't it actually

:P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.