Results 1 to 12 of 12
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calculator program

    Can someone take a look at my calculator program for me. It isn't working properly. I know very little about vb and tried to copy a program for an online course. Thanks.

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Calculator program

    Hi Melanie,

    Could you post the code of your program so that somebody can take a look and maybe advise you.

    Andrew C

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calculator program

    OK - dumb question - do I just copy and paste everything in this area.

  4. #4
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Calculator program

    Melanie,

    You can copy the code and paste, unless it is very extensive, in which case you might attach it as file. Is it VB or VBA ?.

    If VBA you could attach the odcument containing the code, e.g. Excel workbook or Word document etc.

    But it is probably ok to copy and paste either way.

    Andrew

  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calculator program

    ok - here it is in VB - thanks - I will try attaching as well.
    Option Explicit
    Dim Op1, Op2
    Dim Decimalflag As Integer
    Dim NumOps As Integer
    Dim LastInput
    Dim OpFlag
    Dim TempReadout


    'click for cancel key
    'reset display

    Private Sub Cancel_Click()
    ReadOut = Format(0, "0.")
    Op1 = 0
    Op2 = 0
    Form_Load

    End Sub

    'click for cancel entry

    Private Sub CancelEntry_Click()
    ReadOut = Format(0, "0.")
    Decimalflag = False
    LastInput = "CE"
    End Sub

    'event for decimal
    'if last key was operator, initialize
    'readout to "0" otherwise decimal point
    Private Sub Decimal_Click()
    If LastInput = "NEG" Then
    ReadOut = Format(0, "-0")
    ElseIf LastInput <> "NUMS" Then
    ReadOut = Format(0, "0.")
    End If
    Decimalflag = True
    LastInput = "NUMS"
    End Sub

    'routine for form
    'set variables to initial values

    Private Sub Form_Load()
    Decimalflag = False
    NumOps = 0
    LastInput = "NONE"
    OpFlag = ""
    ReadOut = Format(0, "0.")
    Decimal.Caption Format(0, "0.")

    End Sub

    'click for number keys 0-9
    'append new number to display

    Private Sub Number_Click(Index As Integer)
    If LastInput <> "NUMS" Then
    ReadOut = Format(0, ".")
    Decimalflag = False
    End If
    If Decimalflag Then
    ReadOut = ReadOut + Number(Index).Caption
    Else
    ReadOut = Left(ReadOut, InStr(ReadOut, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")

    End If
    If LastInput = "NEG" Then ReadOut = "-" & ReadOut
    LastInput = "NUMS"
    End Sub

    'click for operator keys
    'if preceeding keypress was number, increments numops.
    'If one operand is pesent, setp Op1, if two
    'set op1 equal to result of oreation on o1 and current input string, display result

    Private Sub Operator_Click(Index As Integer)
    TempReadout = ReadOut
    If LastInput = "NUMS" Then
    NumOps = NumOps + 1
    End If
    Select Case NumOps
    Case 0
    If operator(Index).Caption = "-" And LastInput <> "NEG" Then
    ReadOut = "-" & ReadOut
    LastInput = "NEG"
    End If
    Case 1
    Op1 = ReadOut
    If operator(Index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "-" Then
    ReadOut = "-"
    LastInput = "NEG"
    End If
    Case 2
    Op2 = TempReadout
    Select Case OpFlag
    Case "+"
    Op1 = CDbl(Op1) + CDbl(Op2)
    Case "-"
    Op1 = CDbl(Op1) - CDbl(Op2)
    Case "X"
    Op1 = CDbl(Op1) * CDbl(Op2)
    Case "/"
    If Op2 = 0 Then
    MsgBox "Can't divide by zero", 48, "Calculator"
    Else
    Op1 = CDbl(Op1) / CDbl(Op2)
    End If
    Case "="
    Op1 = CDbl(Op2)
    Case "%"
    Op1 = CDbl(Op1) * CDbl(Op2)
    End Select
    ReadOut = Op1
    NumOps = 1
    End Select
    If LastInput <> "NEG" Then
    LastInput = "OPS"
    OpFlag = operator(Index).Caption
    End If

    End Sub

    'Click for %
    'Compute and display % of operand
    Private Sub Percent_click()
    ReadOut = ReadOut / 100
    LastInput = "Ops"
    OpFlag = "%"
    NumOps = NumOps + 1
    Decimalflag = True
    End Sub
    Attached Files Attached Files

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Calculator program

    What the heck is ReadOut? You keep referencing it in your code, but you don't include the code for that function or sub. If you didn't copy that part from your course work, that may be why it isn't working.
    Charlotte

  7. #7
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calculator program

    This is where I got the code. I just typed everything (didn't cut and paste)
    <A target="_blank" HREF=http://www.engin.umd.umich.edu/CIS/course.des/cis400/vbasic/calcode.txt>http://www.engin.umd.umich.edu/CIS/course....sic/calcode.txt</A>

    I understand now what Andrew was saying, the numeric keys weren't told to do anything. Geeze - I thought this was going to be easier. I did the hangman and tic tac toe with few problems.

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Calculator program

    OK, Readout is a label and you're trying to put the value of the calculation into it or to clear it. I don't know why the original worked, but then I was taught rather careful (and readable) VB. Try changing "Readout =" in the code to "Readout.Caption = " and see if that makes a difference. Also change "ReadOut = ReadOut + Number(Index).Caption" to "Readout.Caption = Val(Readout.Caption) + Val(Number(Index).Caption)".

    This is difficult code to read because they've decided to use the default properties instead of specifying them and to allow VB to "figure out" that it needs to coerce strings into numbers. If you clean it up like this, you may be able to identify what isn't working.
    Charlotte

  9. #9
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Calculator program

    Melanie, can I ask if it was recommended to use this code, or did you just find it yourself ?

    I ask because I think it involves (though I'm not sure, as I have not tried it) custom ActiveX controls and/or class modules, which might be too much for a beginner.

    Try this <A target="_blank" HREF=http://www.planet-source-code.com/vb/scripts/BrowseCategoryOrSearchResults.asp?lngWId=1&?lngWId =1&grpCategories=&txtMaxNumberOfEntriesPerPage=10& optSort=Alphabetical&chkThoroughSearch=&blnTopCode =False&blnNewestCode=False&blnAuthorSearch=False&l ngAuthorId=&strAuthorName=&blnResetAllVariables=&b lnEditCode=False&mblnIsSuperAdminAccessOn=False&in tFirstRecordOnPage=11&intLastRecordOnPage=20&intMa xNumberOfEntriesPerPage=10&intLastRecordInRecordse t=133&chkCodeTypeZip=&chkCodeDifficulty=&chkCodeTy peText=&chkCodeTypeArticle=&txtCriteria=calculator >Link</A>, where you might find something more suitable. Try and find something that not only works for you, but something you can understand fully, as you will learn a lot more that way.

    Andrew

  10. #10
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Calculator program

    I didn't pick up on an activex control in there, but it definitely does use a control array, which is almost equally confusing to beginners.
    Charlotte

  11. #11
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calculator program

    Thank you - yes charlotte - I do remember the control arrays - that was my next question. I keep staring and this code and wondering about the command buttons for the numeric keys - there is nothing in the code that refers to cmd0-cmd9.

    attached is the latest program with the changes you suggested.

    Are you the same charlotte who used to be on a wordperfect support list years ago?? just wondering, If so - you helped me out long, long ago....
    Attached Files Attached Files

  12. #12
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Calculator program

    I doubt it. I don't remember ever being on a WordPerfect list, although the same can't be said in reverse. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Your form has a few problems. First of all, there are two control arrays referenced in your code and none on your form. Your number buttons should each be named Number if you want to make the code work, and each should have an index value that's the same as the number on its caption. So the 1 button should be named Number and its index should be set to 1. The 2 button should be named Number and its index should be set to 2, etc. These buttons have to be part of the Number control array. When you click one of them, the Number_Click() sub runs and grabs the index of the control you clicked to carry on from there.

    The same is true of the +, -, *, / and = buttons. They are all part of the Operator control array and should all be named Operator, with the minus button as 0 index and the others numbered sequentially, although the order doesn't really matter to the program.

    Once you've made those changes, the code should run. At least, it runs on my machine. I'm attaching your repaired form so you can study it. I did add a form_unload routine to end the project. That will keep you from running out of memory if you keep playing with the application.
    Attached Files Attached Files
    Charlotte

Posting Permissions

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