Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that mimicks a simple calculator that can do adding, multiplication, or modulo of 2 integers. The program takes 3 arguments at the

Write a program that mimicks a simple calculator that can do adding, multiplication, or modulo of 2 integers.

The program takes 3 arguments at the command line: an integer, a character, and another integer. The character can be one of only 3 types: '+', 'x', or '%'. The program then returns either the sum, the product, or the modulo of the 2 integers.

The program should be able to verify that:

1) the user has exactly 3 arguments. 2) the operator used is one of the 3 allowed operators and nothing else. 3) when using modulo, the second integer is not zero (otherwise, you would divide by zero).

For each of these conditions, the program should print out a specific error message (called USAGE messages) and exit. For each condition, the error message should be:

1) Number of arguments is incorrect. 2) Bad operation choice. 3) Cannot divide by zero.

Hint: you have to use cerr instead of cout to output the usage messages. Do not use cerr for any other outputs the program makes. While cerr and cout are alike in that they direct values to standard output, we usually use cout for the standard output, but use cerr to show or report errors to the user and the system.

You may use the exit(1) statement when exiting from giving some of the error messages. The instructor will demonstrate the proper use of exit(1) and cerr in class on Tuesday.

This program does not loop back to take inputs again. See the sample run examples below.

image text in transcribed

Number of arguments is incorrect. $./calculate 1+ 2 $ 3 $-13 $-35 $ 2 $ ./calculate 4 3 2 0 Number of arguments is incorrect. s ./calculate 5 - 4 Bad operation choice. $ . /calculate 8 % 0 Cannot divide by zero

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions

Question

What Time of the Day Am I Most Productive? (I. E. 2.)

Answered: 1 week ago