1 Replies - 1848 Views - Last Post: 02 August 2007 - 06:32 AM Rate Topic: -----

#1 carlMan5000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-August 07

help with vb6 windows calculator

Post icon  Posted 01 August 2007 - 02:41 PM

im pretty new to vb6. i have two problems with my calculator. first, when i divide by zero
the calculator does not show the error message i have set it show, instead it shows 0.
my second problem is that after i click the equal sign, and click a number, the number is added on to the answer
instead of clearing the answer and putting in the number.
for example, if i do 9+9, 18 shows after i click the equal sign. if i click 1 instead of showing 1, it shows 181. help is much needed. here is my whole calcultor code.
Dim calcSaved As Double, calcResult As Double, calcOp As String

Private Sub cmdCalc_Click(Index As Integer)
	Dim calcBut As String, calcDisplay As String
	calcBut = cmdCalc(Index).Caption
	calcDisplay = lblCalc.Caption
  
	
	Select Case calcBut
		Case "0", "1", "2", "3", "4", _
			  "5", "6", "7", "8", "9"
			calcDisplay = calcDisplay & calcBut
			lblCalc.Caption = calcDisplay
		Case "."
			If (InStr(calcDisplay, ".") > 0) Then
				calcDisplay = calcDisplay & calcBut
			Else
				calcDisplay = calcDisplay & "."
			End If
			lblCalc.Caption = calcDisplay
		Case "+/-"
			If (calcDisplay <> "") Then
				If Left(calcDisplay, 1) = "-" Then
					calcDisplay = Replace(calcDisplay, "-", "")
				Else
					calcDisplay = "-" & calcDisplay
				End If
			End If
			lblCalc.Caption = calcDisplay
		Case "+", "-", "x", "/"
			calcSaved = Val(calcDisplay)
			lblCalc.Caption = ""
			calcOp = calcBut
		Case "="
			calcResult = Val(calcDisplay)
			Select Case calcOp
				Case "+"
					calcResult = calcResult + calcSaved
				Case "-"
					calcResult = calcSaved - calcResult
				Case "x"
					calcResult = calcSaved * calcResult
		   [color=#3333FF]	 Case "/"	
					If calcResult = 0 Then
					   
					   calcDisplay = "error"
					Else
					calcResult = calcSaved / calcResult
				 
					End If
				Case Else
					calcResult = calcDisplay
			End Select
			lblCalc.Caption = calcResult[/color]
		Case "BackSpace"
			If Val(calcDisplay) <> 0 Then
				calcDisplay = Left(calcDisplay, Len(calcDisplay) - 1)
				calcSaved = Val(calcDisplay)
				lblCalc.Caption = calcDisplay
			End If
		Case "Clear"
			calcDisplay = ""
			calcResult = 0
			calcSaved = 0
			lblCalc.Caption = calcDisplay
	End Select

	
	
		
End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: help with vb6 windows calculator

#2 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 12
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: help with vb6 windows calculator

Posted 02 August 2007 - 06:32 AM

The error is in this portion of code:

Quote

Case "/"	
					If calcResult = 0 Then
					   
					   calcDisplay = "error"
					Else
					calcResult = calcSaved / calcResult
				 
					End If



You never calcualte the result before you check the result of the calculation. Add a statement to calculate result like you did in the other operations, and then check to see if it's 0. As it is, the calculated result will never be 0 unless it's the first one (calculate result or any numeric data type is automatically set to 0 when you first run any vb program)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1