none
Hey guys I need help with my program that is not working, not doing the calculations RRS feed

  • Question

  • Here is the code

    i want it simple math like it is there, but its not working plus it need's to be displayed in msgboxes.

    thank you a lot

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            Dim Title As String
            Dim Books As Integer
            Dim Costperbook As Single
            Dim Totalcost As Decimal
            Dim Voucher As String
            Dim DiscountRate As Single
            Dim Discount As Decimal
            Dim DiscountVoucher As Integer

            Costperbook = 15.99
            DiscountVoucher = 25

            txtTitle.Text = InputBox("Please Enter your book name")
            txtBooks.Text = InputBox("Please Enter Amount you'd like to buy")
            txtCostperbook.Text = InputBox("Please Enter the cost of the book", 15.99)
            txtVoucher.Text = InputBox("Please Say yes or no")

            Title = txtTitle.Text
            Books = txtBooks.Text
            Voucher = txtVoucher.Text
            txtTotalcost.Text = Totalcost



            Books = InputBox("You cannot order less than 1 book or  more than 80", "Enter Amount")
            Do While Books <= 1 Or Books >= 80
                MsgBox("Your entry is out of range please choose amount from 1-80")
                Books = InputBox("You cannot order less than 1 book or  more than 80", "Enter Amount")
            Loop

            Voucher = InputBox("Please enter Y or y if you are using a voucher if not Enter N or n if you are not using voucher", "Pick an answear")
            Do Until Voucher = "Y" Or Voucher = "y" Or Voucher = "N" Or Voucher "n"
                MsgBox("Voucher incorrect, Please try again")
                Voucher = ("Please try again")

            Loop

            If Books >= 5 Or Books <= 10 Then
                DiscountRate = 0.05
            ElseIf Books >= 11 Or Books <= 50 Then
                DiscountRate = 0.075
            ElseIf Books >= 51 Then
                DiscountRate = 0.1
            End If

            If Voucher = "Y" Or Voucher = "y" Then
                Discount = ((Costperbook * Books) * DiscountRate)
                Totalcost = Format((Costperbook * Books) - Discount - 25)
                txtTotalcost.Text = Totalcost

            End If

            If Voucher = "N" Or Voucher = "n" Then
                Totalcost = Books * Costperbook
            End If


        End Sub

        Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
            txtBooks.Clear()
            txtCostperbook.Clear()
            txtVoucher.Clear()
            txtTotalcost.Clear()

        End Sub
    End Class

    Tuesday, February 13, 2018 11:45 PM

Answers

  • Hi Maniiii,

    I made some changes to your code, please take a look:

    Dim Title As String
        Dim Books As Integer
        Dim Costperbook As Single
        Dim Totalcost As Decimal
        Dim Voucher As String
        Dim DiscountRate As Single
        Dim Discount As Decimal
        Dim DiscountVoucher As Integer
    
    
        Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Costperbook = 15.99
            DiscountVoucher = 25
    
            txtTitle.Text = InputBox("Please Enter your book name")
    
            txtBooks.Text = InputBox("Please Enter Amount you'd like to buy")
            If txtBooks.Text >= 80 Or txtBooks.Text < 1 Then
                MessageBox.Show("You cannot order less than 1 book or  more than 80", "Enter Amount")
                txtBooks.Text = InputBox("Please Enter Amount you'd like to buy")
            End If
    
            txtCostperbook.Text = InputBox("Please Enter the cost of the book", 15.99)
            If txtCostperbook.Text < 0 Then
                MessageBox.Show("Please enter the correct price!")
                txtCostperbook.Text = InputBox("Please Enter the cost of the book", 15.99)
            End If
    
            txtVoucher.Text = InputBox("Please enter Y or N")
            If txtVoucher.Text.ToUpper = "Y" Or txtVoucher.Text.ToUpper = "N" Then
            Else
                MessageBox.Show("Please enter Y or y if you are using a voucher if not Enter N or n if you are not using voucher")
                txtVoucher.Text = InputBox("Please enter Y or N")
            End If
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Title = txtTitle.Text
            Books = txtBooks.Text
            Voucher = txtVoucher.Text
            txtTotalcost.Text = Totalcost
    
    
            If Books >= 1 And Books < 5 Then
                DiscountRate = 0.025
            ElseIf Books >= 5 And Books <= 10 Then
                DiscountRate = 0.05
            ElseIf Books >= 11 And Books <= 50 Then
                DiscountRate = 0.075
            ElseIf Books >= 51 Then
                DiscountRate = 0.1
            End If
    
            If Voucher.ToUpper = "Y" Then
                Discount = Costperbook * Books * DiscountRate
                txtTotalcost.Text = (Costperbook * Books) - Discount - 25
            End If
            If Voucher.ToUpper = "N" Then
                txtTotalcost.Text = Books * Costperbook
            End If
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
            txtTitle.Clear()
            txtBooks.Clear()
            txtCostperbook.Clear()
            txtVoucher.Clear()
            txtTotalcost.Clear()
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Maniiii Thursday, February 15, 2018 7:04 PM
    Wednesday, February 14, 2018 7:00 AM
    Moderator

All replies

  • Dim Title As String
            Dim Books As Integer
            Dim Costperbook As Single
            Dim Totalcost As Decimal
            Dim Voucher As String

    These are my txt boxes

    http://scr.hu/2gs/on1mx

    Tuesday, February 13, 2018 11:47 PM
  • First off, place the following line as the first line in your form

    Option Strict On

    Next 

    Option Infer Off

    You will get a lot of errors from the line above that will need correcting.

    Next, forget using InputBox and instead create a modal form with TextBox controls where you validate any value in a TextBox using Integer.TryParse for integers, Decimal.TryParse, Single.TryParse (and there is no good reason for using Single).


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, February 13, 2018 11:55 PM
    Moderator
  • Hi

    Am I mistaken when I think you already asked this question in a previous thread? If I am right, didn't you mark some of the posts as answers to your question? How does this thread differ from the other one?

    Also, your point about 'but its not working' is not good enough - you need to be more specific about where/what is not working - what the variable value were (using breakpoints etc).

    BTW: when you post code, please put it in a code block (tool in the toolbar).


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, February 14, 2018 12:02 AM
    Wednesday, February 14, 2018 12:00 AM
  • i want it simple math like it is there, but its not working plus it need's to be displayed in msgboxes.

    You need to describe what you mean by 'not working'.   Describe what happens when you use this code with the values that you are entering to test the code.  Show what you think should happen when you enter those values.

    Note that you have not considered that the InputBox method always returns a string - you are assuming that the runtime can always successfully convert whatever the enters for 'books' to an integer. There are several other places where you assume that the conversion between string and number will work the way you want.   You should not rely on the default - those conversions should be done explicitly.

    What should be displayed in messageboxes?


    • Edited by Acamar Wednesday, February 14, 2018 12:04 AM fmt
    Wednesday, February 14, 2018 12:03 AM
  • Yeah but it needs to be the way i have it. If any of you could correrct my code id appreciate it.

    Wednesday, February 14, 2018 12:10 AM
  • Yeah but it needs to be the way i have it. If any of you could correrct my code id appreciate it.
    Wednesday, February 14, 2018 12:10 AM
  • When i use the code it crashes, i've tried to fix it but can't seem to find the problem
    Wednesday, February 14, 2018 12:11 AM
  • In msgboxes it needs to display "you ordered amount of books ordered title, total cost.
    Wednesday, February 14, 2018 12:14 AM
  • I can send the program via email if someone want's it
    Wednesday, February 14, 2018 12:16 AM
  • When i use the code it crashes, i've tried to fix it but can't seem to find the problem
    What is the error message?  What line of code does the error occur at?
    Wednesday, February 14, 2018 12:27 AM
  • When i use the code it crashes, i've tried to fix it but can't seem to find the problem

    What is the error message?  What line of code does the error occur at?
           BC30205 The end of the instruction was expected
    Wednesday, February 14, 2018 12:37 AM
  • When i use the code it crashes, i've tried to fix it but can't seem to find the problem

    What is the error message?  What line of code does the error occur at?

           BC30205 The end of the instruction was expected
    My bad line 33

    • Edited by Maniiii Wednesday, February 14, 2018 12:41 AM wrong
    Wednesday, February 14, 2018 12:38 AM
  • When i use the code it crashes, i've tried to fix it but can't seem to find the problem

    Hi

    Not surprising. Why did you ignore my post above?

    *

    One example of one of your errors:

    You use: Books = txtBooks.Text

    but, you declared Books as an Integer and txtBooks.text is a string!

    so, now you are saying Books = a String - that won't work!

    *

    Any String that needs to be used in a calculation, or assigned to a numeric type needs to be converted to the appropriate type.
    For example,  Books = CInt(txtBooks.Text) would covert the string to an Integer, provided the user had the sense to enter numbers into the Input box. If they entered any letters, then they can't be converted to an Integer and you are back to square 1.

    *

    So, think of this:

    If you had a function that you could call with a string value and the function would return the correct numeric type that can be used as a number -then wouldn't that be great?

    *

    Such functions are straightforward to produce. Just lookup Integer.TryParse and/or Double.TryParse and/or Decimal.TryParse etc. They would solve at least one of your problems.


    Regards Les, Livingston, Scotland

    Wednesday, February 14, 2018 12:45 AM
  • My bad line 33

    The code you have posted does not have line numbers, so '33' is not useful.  What is the line of code? 

    The error message indicates that the syntax is unrecognizable - the compiler does not understand what you are trying to do.

    Wednesday, February 14, 2018 12:45 AM
  • My bad line 33

    The code you have posted does not have line numbers, so '33' is not useful.  What is the line of code? 

    The error message indicates that the syntax is unrecognizable - the compiler does not understand what you are trying to do.

    line 33 Do Until Voucher = "Y" Or Voucher = "y" Or Voucher = "N" Or Voucher "n"
    Wednesday, February 14, 2018 12:55 AM
  • line 33 Do Until Voucher = "Y" Or Voucher = "y" Or Voucher = "N" Or Voucher "n"
    You are missing the '=' operator for that last comparison.
    Wednesday, February 14, 2018 1:04 AM
  • line 33 Do Until Voucher = "Y" Or Voucher = "y" Or Voucher = "N" Or Voucher "n"

    You are missing the '=' operator for that last comparison.
    Would you be able to fix it?
    Wednesday, February 14, 2018 1:23 AM
  • Would you be able to fix it?
    You can fix it for yourself - insert the = sign where it is required between the variable and the value you are testing the variable for.
    Wednesday, February 14, 2018 1:26 AM
  • Hi Maniiii,

    I made some changes to your code, please take a look:

    Dim Title As String
        Dim Books As Integer
        Dim Costperbook As Single
        Dim Totalcost As Decimal
        Dim Voucher As String
        Dim DiscountRate As Single
        Dim Discount As Decimal
        Dim DiscountVoucher As Integer
    
    
        Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Costperbook = 15.99
            DiscountVoucher = 25
    
            txtTitle.Text = InputBox("Please Enter your book name")
    
            txtBooks.Text = InputBox("Please Enter Amount you'd like to buy")
            If txtBooks.Text >= 80 Or txtBooks.Text < 1 Then
                MessageBox.Show("You cannot order less than 1 book or  more than 80", "Enter Amount")
                txtBooks.Text = InputBox("Please Enter Amount you'd like to buy")
            End If
    
            txtCostperbook.Text = InputBox("Please Enter the cost of the book", 15.99)
            If txtCostperbook.Text < 0 Then
                MessageBox.Show("Please enter the correct price!")
                txtCostperbook.Text = InputBox("Please Enter the cost of the book", 15.99)
            End If
    
            txtVoucher.Text = InputBox("Please enter Y or N")
            If txtVoucher.Text.ToUpper = "Y" Or txtVoucher.Text.ToUpper = "N" Then
            Else
                MessageBox.Show("Please enter Y or y if you are using a voucher if not Enter N or n if you are not using voucher")
                txtVoucher.Text = InputBox("Please enter Y or N")
            End If
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Title = txtTitle.Text
            Books = txtBooks.Text
            Voucher = txtVoucher.Text
            txtTotalcost.Text = Totalcost
    
    
            If Books >= 1 And Books < 5 Then
                DiscountRate = 0.025
            ElseIf Books >= 5 And Books <= 10 Then
                DiscountRate = 0.05
            ElseIf Books >= 11 And Books <= 50 Then
                DiscountRate = 0.075
            ElseIf Books >= 51 Then
                DiscountRate = 0.1
            End If
    
            If Voucher.ToUpper = "Y" Then
                Discount = Costperbook * Books * DiscountRate
                txtTotalcost.Text = (Costperbook * Books) - Discount - 25
            End If
            If Voucher.ToUpper = "N" Then
                txtTotalcost.Text = Books * Costperbook
            End If
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
            txtTitle.Clear()
            txtBooks.Clear()
            txtCostperbook.Clear()
            txtVoucher.Clear()
            txtTotalcost.Clear()
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Maniiii Thursday, February 15, 2018 7:04 PM
    Wednesday, February 14, 2018 7:00 AM
    Moderator
  • Everything is working except the total is not showing
    Wednesday, February 14, 2018 7:22 PM
  • Everything is working except the total is not showing
    Do you mean that there is nothing displayed in the text box txtTotalcost?   Insert a breakpoint at the line of code where you update the textbox text, and make sure that line is executed.   If it is, look at the value of the variables on that line.    What is the data that you are using to test, and what is the result that you expect to get?
    Wednesday, February 14, 2018 9:09 PM
  • Everything is working except the total is not showing

    Do you mean that there is nothing displayed in the text box txtTotalcost?   Insert a breakpoint at the line of code where you update the textbox text, and make sure that line is executed.   If it is, look at the value of the variables on that line.    What is the data that you are using to test, and what is the result that you expect to get?
    for example 50 books times 15.99 - 25. it gives me empty box
    Wednesday, February 14, 2018 11:23 PM
  • for example 50 books times 15.99 - 25. it gives me empty box
    When you inserted a breakpoint at the lines where the text box text gets updated, and the debugger stopped at the line, what were the values of the variables used in the calculation?
    Thursday, February 15, 2018 2:02 AM
  • for example 50 books times 15.99 - 25. it gives me empty box

    When you inserted a breakpoint at the lines where the text box text gets updated, and the debugger stopped at the line, what were the values of the variables used in the calculation?
    Nvm i got it to work, thank you all
    Thursday, February 15, 2018 7:05 PM