Create a calculator program that works as follows: 1. Your program should read a
ID: 3658991 • Letter: C
Question
Create a calculator program that works as follows: 1. Your program should read an integer, an arithmetic operation, and another integer in that order. 2. Valid arithmetic operations are +, *, -, and /. 3. Output will depend on which arithmetic operation is performed: + Print the sum of the numbers. * Print the product of the numbers. Print the second number subtracted from the rst. = Print the rst number divided by the second. 4. If the arithmetic operation is not one of the valid operations, print the error message Error: invalid operation and return 1. 5. If the arithmetic operation is division and the second integer is zero, print the error message Error: division by zero and return 1. 6. Use integer arithmetic only. 7. No single input string is required to be longer than 20 characters.Explanation / Answer
You need to use ReadKey differently. ReadKey captures a key-stroke from the keyboard, and passes back a character (value) of that key. (For those more technical, I know it's more complex than this, but bear with me). In your case you need to capture the users keypress and then test it as follows: var ch: char; ... write('press Y for +, or N for -'); ch:=ReadKey; case UpCase(ch) of 'N': c:=a-b; 'Y': c:=a+b; else writeLn('Please select either "Y" or "N" only.'); end; {case} writeln('the result is: ',c); You can also place the capture and test in a loop, until a valid answer is input. na Source(s): Please remember to award best answer if it works for you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.