English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am suppose to write a pseudocode for a calculator problem. This is what I got... The only thing I think I am really missing is "Your project should include a function/subprogram to check for a divide by zero error if the chosen operator is division." How do I do this?

function main(){

input();
calculation();
result();
} End main

function input(){
var number1 integer starts with 0
var number2 integer starts with 0
var operator string starts with ""

Ask for the first number, store in number1
Ask for the second number, store in number 2
Ask for the operator sign, store in operator
} End input

function calculation(){
if (operator == '+') then {result = number1 + number2;}
} End If
if (operator == '-') then {result = number1 - number2;}
} End If
if (operator == '*') then {result = number1 * number2;}
} End If
if (operator == '/') then {result = number1 / number2;}
} End If
} End calculation

function result(){
alert(result)
} End result

2007-11-02 09:54:47 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

so I will need to make a new variable for it? var zeroDivide = 0?

2007-11-02 10:09:59 · update #1

2 answers

function divide(var numerator, var denominator)
{
if denominator == 0
// raise an error ??
else
return numerator / denominator
}

in your existing code:
if (operator == '/') then {result = divide(number, number2)}

2007-11-02 10:32:41 · answer #1 · answered by Anonymous · 0 0

You want to do something like

if (zeroDivide()) {
show error message
} else {
calculation()
}

The function zeroDivide will return either true of false.
I think you can do the rest.
Good luck.

2007-11-02 17:06:35 · answer #2 · answered by AnalProgrammer 7 · 0 1

fedest.com, questions and answers