10 Replies - 1477 Views - Last Post: 28 February 2014 - 09:46 AM Rate Topic: -----

#1 HiTechRedneck3   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Making Calculations Based on Text File

Posted 28 February 2014 - 12:38 AM

In the Visual Basic program I am working on, I am to perform a set of calculations based on the sum of the values read in from a text file named profit.txt.

The directions for the portion of the program that I am stuck on are as follows.

When "Compute->Compute Profit" (menu buttons) is clicked, the following occurs: a. If the file has not been opened and summed yet, give the user a warning pop-up indicating that. Presume zero profit from the file if it has not been read in yet. b. If any of the "amount" text boxes are empty, then presume zero in those fields. (There are check boxes for Wages, Rent, and Utilities, and text boxes labeled Wages Amount, Rent Amount, and Utilities Amount). c. For every check box selected in the "overhead" group box, subtract the values from the appropriate "amount" text boxes from the total sum calculated from the file. d. Display the total profit at the bottom of the form. (I actually know how to do this one).

So far I have gotten the program to read the file and tested it to make sure it is calculating the sum correctly. But I am having trouble figuring out how to do the steps listed above (aside from displaying the total at the bottom). Here is the code I have written so far.

Option Strict On

Imports System.IO

Public Class Form1

    Dim sum As Double = 0

    Private Sub menOpen_Click(sender As Object, e As EventArgs) Handles menOpen.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "text files |*.txt|All Files|*.*"
        ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim selectedFileName As String = System.IO.Path.GetFileName(ofd.FileName)
            If selectedFileName.ToLower = "profit.txt" Then
                Dim line As String
                Using reader As New StreamReader(ofd.OpenFile)
                    While Not reader.EndOfStream
                        line = reader.ReadLine
                        Dim value As Integer
                        If Integer.TryParse(line, value) Then
                            sum = sum + value
                        End If
                        Console.WriteLine(line)
                    End While
                End Using
            Else
                MessageBox.Show("You cannot use that file!")
            End If
        End If
    End Sub

    Private Sub menExit_Click(sender As Object, e As EventArgs) Handles menExit.Click
        Me.Close()
    End Sub

    Private Sub menComputeProfit_Click(sender As Object, e As EventArgs) Handles menComputeProfit.Click
 If 
    End Sub
End Class 


Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Making Calculations Based on Text File

#2 belgarion262   User is offline

  • Prince of all (2) Saiyans

Reputation: 172
  • View blog
  • Posts: 955
  • Joined: 25-October 09

Re: Making Calculations Based on Text File

Posted 28 February 2014 - 01:43 AM

Forgive me, it's early in the morning for me, but is there an error occurring or is it merely that the desired result is not happening?
Was This Post Helpful? 0
  • +
  • -

#3 HiTechRedneck3   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Making Calculations Based on Text File

Posted 28 February 2014 - 01:58 AM

A combination of both actually. I have worked with the code some more since posting this and will update it with the current code and the error I am receiving.

Here is the current code.
 Option Strict On

Imports System.IO

Public Class Form1

    Dim sum As Double
    Dim fileRead As Boolean
    Dim profit As Double

    Private Sub menOpen_Click(sender As Object, e As EventArgs) Handles menOpen.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "text files |*.txt|All Files|*.*"
        ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim selectedFileName As String = System.IO.Path.GetFileName(ofd.FileName)
            If selectedFileName.ToLower = "profit.txt" Then
                Dim line As String
                Using reader As New StreamReader(ofd.OpenFile)
                    While Not reader.EndOfStream
                        line = reader.ReadLine
                        Dim value As Integer
                        If Integer.TryParse(line, value) Then
                            sum = sum + value
                            fileRead = True
                        End If
                        Console.WriteLine(line)
                    End While
                End Using
            Else
                MessageBox.Show("You cannot use that file!")
            End If
        End If
    End Sub

    Private Sub menExit_Click(sender As Object, e As EventArgs) Handles menExit.Click
        Me.Close()
    End Sub

    Private Sub menComputeProfit_Click(sender As Object, e As EventArgs) Handles menComputeProfit.Click
        If fileRead = False Then
            MessageBox.Show("The file profit.txt has not been read in yet, the profit will be set to zero.")
            sum = 0
        End If

        If chkWages.Checked Then
            profit = CDbl(("$" & Val(sum) - Val(txtWages.Text)))
        End If
        If chkRent.Checked Then
            profit = CDbl(("$" & Val(sum) - Val(txtRent.Text)))
        End If
        If chkUtilities.Checked Then
            profit = CDbl(("$" & Val(sum) - Val(txtUtilities.Text)))
        End If

    End Sub

    Private Sub radSales_CheckedChanged(sender As Object, e As EventArgs) Handles radSales.CheckedChanged
        If radSales.Checked Then
            profit = sum
        End If
    End Sub

    Private Sub radSandO_CheckedChanged(sender As Object, e As EventArgs) Handles radSandO.CheckedChanged
        If radSandO.Checked Then
            profit = (sum - CDbl(txtWages.Text) - CDbl(txtRent.Text) - CDbl(txtUtilities.Text))
        End If

    End Sub

    Private Sub txtAnswer_TextChanged(sender As Object, e As EventArgs) Handles txtAnswer.TextChanged
        txtAnswer.Text = CStr(profit)
    End Sub
End Class 


And here is the error I am receiving
 Conversion from string "" to type 'Double' is not valid. 


The program crashes when I click the Sales and Overhead radio button (radSandO) and is occurring at this portion of the code.
 profit = (sum - CDbl(txtWages.Text) - CDbl(txtRent.Text) - CDbl(txtUtilities.Text)) 

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6700
  • View blog
  • Posts: 27,542
  • Joined: 12-December 12

Re: Making Calculations Based on Text File

Posted 28 February 2014 - 02:33 AM

Quote

Conversion from string "" to type 'Double' is not valid.

This is quite a clear error message, so what are the values of txtWages, etc., when you click the radio button?
Was This Post Helpful? 0
  • +
  • -

#5 HiTechRedneck3   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Making Calculations Based on Text File

Posted 28 February 2014 - 02:36 AM

Guess I posted too soon, I did figure out how to correct this error, but am now having another issues. The way my code is written, the profit that displays is only correct if a single check box is selected, but I need it to be correct whether one, two, or all three are selected. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#6 belgarion262   User is offline

  • Prince of all (2) Saiyans

Reputation: 172
  • View blog
  • Posts: 955
  • Joined: 25-October 09

Re: Making Calculations Based on Text File

Posted 28 February 2014 - 02:49 AM

change the if statement to
        If (chkWages.Checked) or (chkRent.Checked) or (chkUtilities.Checked) Then
            profit = CDbl(("$" & Val(sum) - Val(txtWages.Text)))
            profit = CDbl(("$" & Val(sum) - Val(txtRent.Text)))
            profit = CDbl(("$" & Val(sum) - Val(txtUtilities.Text)))
        End If


This way if one, any or many are true then the if code will execute.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6700
  • View blog
  • Posts: 27,542
  • Joined: 12-December 12

Re: Making Calculations Based on Text File

Posted 28 February 2014 - 02:57 AM

@belgarion
profit = CDbl(("$" & Val(sum) - Val(txtWages.Text)))

this won't work because of the insertion of the dollar-sign, and only the last of these three statements will give a value to profit as you are over-writing the value each time.
Was This Post Helpful? 0
  • +
  • -

#8 HiTechRedneck3   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Making Calculations Based on Text File

Posted 28 February 2014 - 03:07 AM

View Postandrewsw, on 28 February 2014 - 02:57 AM, said:

@belgarion
profit = CDbl(("$" & Val(sum) - Val(txtWages.Text)))

this won't work because of the insertion of the dollar-sign, and only the last of these three statements will give a value to profit as you are over-writing the value each time.

Yes, I was just about to comment that now the only one being counted is the last statement. Any ideas on how to fix this?
Was This Post Helpful? 0
  • +
  • -

#9 HiTechRedneck3   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Making Calculations Based on Text File

Posted 28 February 2014 - 03:45 AM

Thanks for the suggestion, but when I tried this the profit was always set to the last one value since each instance of profit replaced the existing value. I need the profit to be something like this, if I type 10 into the Wages text box, 20 into the rent text box, and 30 into the utilities text box, and I check the check boxes for wages and rent, the profit would be the sum of the values in the file minus the values in the text boxes of the corresponding check boxes, so in this case (109018-10-20=108988)
Was This Post Helpful? 0
  • +
  • -

#10 belgarion262   User is offline

  • Prince of all (2) Saiyans

Reputation: 172
  • View blog
  • Posts: 955
  • Joined: 25-October 09

Re: Making Calculations Based on Text File

Posted 28 February 2014 - 07:00 AM

View Postandrewsw, on 28 February 2014 - 09:57 AM, said:

@belgarion
profit = CDbl(("$" & Val(sum) - Val(txtWages.Text)))

this won't work because of the insertion of the dollar-sign, and only the last of these three statements will give a value to profit as you are over-writing the value each time.


*facepalm*

This is why I shouldn't post, I miss obvious things :withstupid:
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Making Calculations Based on Text File

Posted 28 February 2014 - 09:46 AM

belgarion262: Continue to post, even I have brain "parps".
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1