• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[VB6] Preventing Division By Zero

Newbie Spellweaver
Joined
Oct 16, 2008
Messages
85
Reaction score
0
I had a thread up before and someone answered me but it was deleted so when I came back for reference it wasn't there. Can someone tell me what had to be done? I remember a pseudo code or something.

Anyway the problem was:

If I run my program and click the calculate command without entering any integers (therefore I am dividing by zero) my program Breaks.

What is the code to prevent division my zero?
 
Custom Title Activated
Loyal Member
Joined
Jun 28, 2007
Messages
2,986
Reaction score
3
Make a function which takes 2 numbers. Check the second number and see if it's zero. If so, throw an exception, if not, divide and return the result.
 
Newbie Spellweaver
Joined
Oct 16, 2008
Messages
85
Reaction score
0
I don't get it @_@ Look at my other post... It has the code.


I don't even know how to write a function....
 
i didnt do this.
Loyal Member
Joined
Jul 19, 2007
Messages
2,023
Reaction score
345
huh so you wanting to Divide by Zero? the code for a calculator is like

text3.text = val(text1.text) / val(text2.text)

is that what u mean??
 
Skilled Illusionist
Loyal Member
Joined
Jun 23, 2007
Messages
310
Reaction score
1
Check whether numOne or numTwo are 0 and if either/both are, just throw an error.

Code:
if(numOne == "0" || numTwo == "0")
{
cout << "Cannot divide by zero.";
} else {
//Contiune to show answer.
}
Just add that code into the section where it checks for division.
 
Banned
Banned
Joined
Jun 24, 2008
Messages
723
Reaction score
1
Code:
public function checkforzero(num1 as string, num2 as string)
if num1 = "0" or num2 = "0" then
msgbox "you can't use 0 - Monsta.", vbexclamation + vbokonly
else
form1.lblanswer.caption = val(num1) / val(num2)
end if
end function

Don't know, just made it up, try it ^^
 
Back
Top