Visual Basic 6.0 Output Display Problem with Variables
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Visual Basic 6.0 Output Display Problem with Variables

  1. #1
    Join Date
    Apr 2013
    Posts
    2

    Visual Basic 6.0 Output Display Problem with Variables

    I am working on an assignment in Visual Basic 6.0. I have to develop a program that will calculate the total trip cost for each student including a 10%
    discount for senior and financial aid.
    When I enter the calculation with no discount applied, the output displays perfectly until I enter the calculations for the discount. When both the
    output displays are in the code, the display for "no discount" shows only the text and not the price. The output for the discount applied still works.
    I don't know what I'm doing wrong that the output does not display properly with no discount. How am I able to have both outputs displayed properly?
    I hope I can get some help in this. I would deeply appreciate it.

    The main problem I'm having is in red.

    Here is the code:
    Option Explicit
    Dim curTotal As Currency
    Dim strTotal As String
    Dim strOutput As String
    Dim curTenPercent As Currency
    Dim curTwentyPercent As Currency
    Dim curTotalDiscount As Currency
    Dim strTotalDiscount As String
    Dim strOutputDiscount As String

    Private Sub cmdCalc_Click()
    'Initialize values to variables
    curTotal = 200

    'Check if Rappelling
    If chkRappel.Value = 1 Then
    curTotal = curTotal + 30
    End If

    'Check if Backpacking
    If chkBackpack = 1 Then
    curTotal = curTotal + 45
    End If

    'Check if Canoeing
    If chkCanoe = 1 Then
    curTotal = curTotal + 25
    End If

    'Display message when no discount applied
    If chkSenior.Value = 0 And _
    chkFinancialAid = 0 Then
    MsgBox ("No Discount Applied.")
    End If

    'Display Message when both Senior and Financial Aid selected
    If chkSenior.Value = 1 And _
    chkFinancialAid = 1 Then
    MsgBox ("You have received a 20% discount.")
    End If

    'Display Message when selected Senior or Financial Aid selected
    If chkSenior.Value = 1 And _
    chkFinancialAid = 0 Then
    MsgBox ("You have received a 10% discount.")
    End If

    If chkSenior.Value = 0 And _
    chkFinancialAid = 1 Then
    MsgBox ("You have received a 10% discount.")
    End If

    'Declare Discount variables
    curTenPercent = 0.1
    curTwentyPercent = 0.2

    'Determine Discount for either Senior or Financial Aid
    If chkSenior.Value = 1 Or _
    chkFinancialAid = 1 Then
    curTotalDiscount = curTotal * curTenPercent
    strTotalDiscount = curTotal - curTotalDiscount
    End If

    'Determine Discount for both Senior and Financial Aid
    If chkSenior.Value = 1 And _
    chkFinancialAid = 1 Then
    curTotalDiscount = curTotal * curTwentyPercent
    strTotalDiscount = curTotal - curTotalDiscount

    End If

    'Display Output with no Discount Applied
    strTotal = Format(curTotal, "$###.00")
    strOutput = "The total price of your trip will be " & strTotal & "."
    lblOutput.Caption = strOutput



    'Display Output with Discount Applied
    strTotalDiscount = Format(strTotalDiscount, "$###.00")
    strOutputDiscount = "The total price of your trip will be " & _
    strTotalDiscount & "."
    lblOutput.Caption = strOutputDiscount

    End Sub

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,781

    Re: Visual Basic 6.0 Output Display Problem with Variables

    Look at the last line of code. This is nto contained within an if statement so it is always going to execute and since you are using different variables then you will never see what you wrote to the label above because your code replaces it on that last line.

    You have some issues with your structure of If statements as well which will cause more than one piece of code to execute in conditions where it should not.

    For example you are checking if A or B and executing code then you are checking if A and B and executing code so if the second is true then the first was also true meaning both blocks execute when really only one of them should ever execute. You are also doing a redundant check on the values checking to see if the value is 1 then checking to see if the value is 0 what you do not have are any else statements which you should be making use of here.

    Then at the bottom of the code you do not check to see if a discount was applied and you display the non discount amount but overwrite it with the discount amount which in some cases is going to be empty and in other cases will retain the value of the last discounted amount calculated.
    Always use [code][/code] tags when posting code.

  3. #3
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,781

    Re: Visual Basic 6.0 Output Display Problem with Variables

    You could replace all of this code.
    Code:
    Private Sub cmdCalc_Click()
    'Initialize values to variables
    curTotal = 200
    
    'Check if Rappelling
    If chkRappel.Value = 1 Then
    curTotal = curTotal + 30
    End If
    
    'Check if Backpacking
    If chkBackpack = 1 Then
    curTotal = curTotal + 45
    End If
    
    'Check if Canoeing
    If chkCanoe = 1 Then
    curTotal = curTotal + 25
    End If
    
    'Display message when no discount applied
    If chkSenior.Value = 0 And _
    chkFinancialAid = 0 Then
    MsgBox ("No Discount Applied.")
    End If
    
    'Display Message when both Senior and Financial Aid selected
    If chkSenior.Value = 1 And _
    chkFinancialAid = 1 Then
    MsgBox ("You have received a 20% discount.")
    End If
    
    'Display Message when selected Senior or Financial Aid selected
    If chkSenior.Value = 1 And _
    chkFinancialAid = 0 Then
    MsgBox ("You have received a 10% discount.")
    End If
    
    If chkSenior.Value = 0 And _
    chkFinancialAid = 1 Then
    MsgBox ("You have received a 10% discount.")
    End If
    
    'Declare Discount variables
    curTenPercent = 0.1
    curTwentyPercent = 0.2
    
    'Determine Discount for either Senior or Financial Aid
    If chkSenior.Value = 1 Or _
    chkFinancialAid = 1 Then
    curTotalDiscount = curTotal * curTenPercent
    strTotalDiscount = curTotal - curTotalDiscount
    End If
    
    'Determine Discount for both Senior and Financial Aid
    If chkSenior.Value = 1 And _
    chkFinancialAid = 1 Then
    curTotalDiscount = curTotal * curTwentyPercent
    strTotalDiscount = curTotal - curTotalDiscount
    
    End If
    
    'Display Output with no Discount Applied
    strTotal = Format(curTotal, "$###.00")
    strOutput = "The total price of your trip will be " & strTotal & "."
    lblOutput.Caption = strOutput
    
    
    
    'Display Output with Discount Applied
    strTotalDiscount = Format(strTotalDiscount, "$###.00")
    strOutputDiscount = "The total price of your trip will be " & _
    strTotalDiscount & "."
    lblOutput.Caption = strOutputDiscount
    End Sub
    With something like this and get the correct results while having more efficient and easier to read code.

    Code:
    Private Sub cmdCalc_Click()
    'Initialize values to variables
    curTotal = 200
    curTenPercent = 0.1
    curTwentyPercent = 0.2
    
    'Check if Rappelling
    If chkRappel.Value = vbChecked Then
        curTotal = curTotal + 30
    End If
    
    'Check if Backpacking
    If chkBackpack = 1 Then
        curTotal = curTotal + 45
    End If
    
    'Check if Canoeing
    If chkCanoe = 1 Then
        curTotal = curTotal + 25
    End If
    
    If chkSenior.Value = vbChecked Then
        If chkFinancialAid.Value = vbChecked Then ' both are checked
            MsgBox ("You have received a 20% discount.")
            curTotalDiscount = curTotal * curTwentyPercent
            strTotalDiscount = Format(curTotal - curTotalDiscount, "$###.00")
        Else ' senior is checked but FinancialAid is not
            MsgBox ("You have received a 10% discount.")
            curTotalDiscount = curTotal * curTenPercent
            strTotalDiscount = Format(curTotal - curTotalDiscount, "$###.00")
        End If
    ElseIf chkFinancialAid.Value = vbChecked Then 'FinancialAid is checked but senior is not
        MsgBox ("You have received a 10% discount.")
        curTotalDiscount = curTotal * curTenPercent
        strTotalDiscount = Format(curTotal - curTotalDiscount, "$###.00")
    Else
        MsgBox ("No Discount Applied.")
        strTotalDiscount = Format(curTotal, "$###.00")
    End If
    
    strOutput = "The total price of your trip will be " & strTotalDiscount & "."
    lblOutput.Caption = strOutput
    
    
    End Sub
    Notice the parts in red and how I have restructured to remove duplicated code and require fewer tests while giving correct output

    There is more that could be removed and make the code more simple but the main thing is the proper use of the If Else and ElseIF structure which is what I attempted to demonstrate in the code above.
    Last edited by DataMiser; April 3rd, 2013 at 01:43 AM.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Apr 2013
    Posts
    2

    Re: Visual Basic 6.0 Output Display Problem with Variables

    Thank you so much for the help I received from both of you. I am starting to understand about my IF and IF...ELSE statements as well as my variables. You both made it more clear to me and I was able to get the program to run. I have a long way from being a programmer, but I am enjoying it immensely.

    Olga

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,873

    Re: Visual Basic 6.0 Output Display Problem with Variables

    Hope it wasn't a classroom assignment.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center