'new to programing and need advice on making a simple pascal calculator
(sorry for bad english and code writing) recently ive been interested in programing because of the subject in my school, my teacher wanted us to learn pascal, so ive been learning pascal for a couple days and interested to make a simple calculator, i want to make a logic so when i put a wrong input variable type the program will say "wrong input' and either loops or end the program
and heres my code
program SimpleCalculator;
uses
crt;
var
numA, numB, result : real;
op, prog : char;
begin
repeat;
clrscr;
TextColor(15);
write('enter 1st number: ');
readln(numA);
//i want to check if numA is a number, if not the program wont continue or loops back
write('enter operation: ');
readln(op);
write('enter 2nd number: ');
readln(numB);
//i want to check if numB is a number, if not the program wont continue or loops back
if (op = '+') then
result := numA + numB;
write('result ', numA : 2 : 1, ' + ', numB : 2 : 1, ' = ', result : 2 : 1);
end
else if (op='-') then
begin
result := numA - numB;
write('result ', numA : 2 : 1, ' - ', numB : 2 : 1, ' = ', result : 2 : 1);
end
else if (op = '*') then
begin
result := numA * numB;
write('result ', numA : 2 : 1, ' * ', numB : 2 : 1, ' = ', result : 2 : 1);
end
else if (op = '/') then
if (numA = 0) then
begin
write('cannot devide by 0');
writeln;
writeln('press (enter) to go back');
write ('input (e/E) to exit : ');
readln(prog);
end
else if (numB = 0) then
begin
write('cannot devide by 0');
writeln;
readln(prog);
end
else
begin
result := numA / numB;
write('result ', numA : 2 : 1, ' / ', numB : 2 : 1, ' = ', result : 2 : 1);
end
else
writeln(op,' is not an operator!');
readln;
writeln('press (enter) to go back');
write ('input (e/E) to exit : ');
readln(prog);
until (prog= 'E') or (prog= 'e');
end.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
