locked
Need Help Please RRS feed

  • Question

  • Need help please!  Here is the assignment:

    You will read in values from a file containing 8 lines.

    If the value is > 10, write it out to another file that you create within your program (don't forget about "append" vs. "overwrite")

    Also write to this output file the sum of all numbers not written to it (i.e. total of values <= 10), with a text heading (see below)

    Sample input file:
    2
    12
    5
    23
    11
    10
    1
    6

    Resulting output file:
    12
    23
    11
    10
    Total of other numbers
    14


    And here is where I am so far.  Please tell me what I have missed.



    Imports

     

    System.IO
    Module Module1
    Sub Main()
    Dim inputpipe As New StreamReader("C:\Users\Church\Desktop\assignment4.txt")
    inputpipe =
    New StreamReader("C:\Users\Church\Desktop\assignment4.txt")
    Dim value As Integer = 0
    Dim outputpipe As New StreamWriter("C:\Users\Church\Desktop\assignment4.txt")
    outputpipe =
    New StreamWriter("C:\Users\Church\Desktop\assignment4.txt")
    If value > 10 Then Console.WriteLine(value)
    If value <= 10 Then
    outputpipe.WriteLine("C:\Users\Church\Desktop\assignment4.txt")
    End If
    value = inputpipe.ReadLine()
    outputpipe.WriteLine(value)
    value = inputpipe.ReadLine()
    outputpipe.WriteLine(value)
    value = inputpipe.ReadLine()
    outputpipe.WriteLine(value)
    value = inputpipe.ReadLine()
    outputpipe.WriteLine(value)
    value = inputpipe.ReadLine()
    outputpipe.WriteLine(value)
    value = inputpipe.ReadLine()
    outputpipe.WriteLine(value)
    value = inputpipe.ReadLine()
    outputpipe.WriteLine(value)
    value = inputpipe.ReadLine()
    outputpipe.WriteLine(value)
    inputpipe.Close()
    outputpipe.Close()
    End Sub
    End
    Module

    Saturday, February 20, 2010 2:09 AM

Answers

  • The assignment doesn't make much sense if you haven't done loops yet, as you can see from the code below.   And it seems odd that you would be doing file I/O before you have covered the more basic concepts of VB.   And the description doesn't agree with the example output, because 10 is not > 10 and should not be written out to the file.  Also, append vs overwrite doesn't seem relevant - you will always create a new file for the output, and there is no appending involved.

    Note that I have changed the file location slightly, and I have called the output file Assignment4a.txt.  Also I have made it a windows forms application - there is no point in trying to deal with console applications for a simple assignment such as this.  You can paste the code into a console appliction if you prefer it. 

    Imports System.IO
    Public Class Form1

        
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
    Dim inputpipe As New StreamReader("C:\Users\Public\assignment4.txt")
            
    Dim outputpipe As New StreamWriter("C:\Users\Public\assignment4a.txt")
            
    Dim value As Integer = 0
            
    Dim Line As String = ""

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            outputpipe.WriteLine("Total of other numbers")
            outputpipe.WriteLine(value.ToString)

            inputpipe.Close()
            outputpipe.Close()

        
    End Sub
    End
     Class

    Saturday, February 20, 2010 3:58 AM
  • Since you are also interested in loop, here is Do while loop which loops through end of input file.

     'use using statement to dispose unmanaged resources(like file here) 
            Using inputpipe As New StreamReader("C:\Users\Church\Desktop\assignment4.txt")
                Using Outputpipe As New StreamWriter("C:\Users\Church\Desktop\assignment4Ans.txt", True) 'true for append, if removed it will overwrite
                    Dim Sum As Double
                    Dim linevalue As Double
                    Do While Not inputpipe.EndOfStream  'loop lines till the end of file
                        linevalue = Val(inputpipe.ReadLine)
                        If linevalue >= 10 Then
                            Outputpipe.WriteLine(linevalue)
                        Else
                            Sum += linevalue
                        End If
                    Loop
                    Outputpipe.WriteLine("Total of other numbers")
                    Outputpipe.WriteLine(Sum.ToString)
                End Using
            End Using


    Arjun Paudel
    Saturday, February 20, 2010 4:49 AM

All replies

  • Consider using some type of loop, like an Until loop so that you aren't hard-coding in reading each line.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Saturday, February 20, 2010 2:19 AM
  • Thanks,

    the problem is that I am a very new student and we have not covered loops yet, but I do believe it would be helpful. However I am not sure if I need to Append or overwrite. Or even if my "If" Statements are correct.
    Saturday, February 20, 2010 2:23 AM
  • The assignment doesn't make much sense if you haven't done loops yet, as you can see from the code below.   And it seems odd that you would be doing file I/O before you have covered the more basic concepts of VB.   And the description doesn't agree with the example output, because 10 is not > 10 and should not be written out to the file.  Also, append vs overwrite doesn't seem relevant - you will always create a new file for the output, and there is no appending involved.

    Note that I have changed the file location slightly, and I have called the output file Assignment4a.txt.  Also I have made it a windows forms application - there is no point in trying to deal with console applications for a simple assignment such as this.  You can paste the code into a console appliction if you prefer it. 

    Imports System.IO
    Public Class Form1

        
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
    Dim inputpipe As New StreamReader("C:\Users\Public\assignment4.txt")
            
    Dim outputpipe As New StreamWriter("C:\Users\Public\assignment4a.txt")
            
    Dim value As Integer = 0
            
    Dim Line As String = ""

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            Line = inputpipe.ReadLine()
            
    If Val(Line) > 10 Then
                outputpipe.WriteLine(Line)
            
    Else
                value += Val(Line)
            
    End If

            outputpipe.WriteLine("Total of other numbers")
            outputpipe.WriteLine(value.ToString)

            inputpipe.Close()
            outputpipe.Close()

        
    End Sub
    End
     Class

    Saturday, February 20, 2010 3:58 AM
  • Since you are also interested in loop, here is Do while loop which loops through end of input file.

     'use using statement to dispose unmanaged resources(like file here) 
            Using inputpipe As New StreamReader("C:\Users\Church\Desktop\assignment4.txt")
                Using Outputpipe As New StreamWriter("C:\Users\Church\Desktop\assignment4Ans.txt", True) 'true for append, if removed it will overwrite
                    Dim Sum As Double
                    Dim linevalue As Double
                    Do While Not inputpipe.EndOfStream  'loop lines till the end of file
                        linevalue = Val(inputpipe.ReadLine)
                        If linevalue >= 10 Then
                            Outputpipe.WriteLine(linevalue)
                        Else
                            Sum += linevalue
                        End If
                    Loop
                    Outputpipe.WriteLine("Total of other numbers")
                    Outputpipe.WriteLine(Sum.ToString)
                End Using
            End Using


    Arjun Paudel
    Saturday, February 20, 2010 4:49 AM