CSCI 240 Spring 2026

Assignment 5
Looping, switch, and using a menu
(100 points)


Due: Friday, February 27 on the autograder and Blackboard by 11:59 PM

Overview

For this assignment, write a program that will perform the following arithmetic operations:

The program should be menu-driven, which means that it allows the user to select an option from a menu and then, based on the option, ask for more information and perform the arithmetic. The menu should resemble the following:

What operation would you like to perform:
  + addition
  - subtraction
  * multiplication
  / division
  ^ number to power
  ! factorial
  q quit

Operation?

where a value of '+' indicates that an addition operation should be performed, '-' indicates subtraction, '*' indicates multiplication, '/' indicates division that results in the quotient and remainder, '^' indicates raising a number to a power, '!' indicates that a factorial operation should be performed, 'q' or 'Q' indicates the user wants to quit the program, and any other character is invalid.

The cpp file that is submitted for grading must be named assign5.cpp.

Basic Program Logic

Create any variables that are needed for the program. At a minimum, the program requires a character variable to hold the menu choice, and two integers to hold the values that will be used in the arithmetic.

Display the menu and ask the user what operation they would like to perform. This is a character value that must be stored in a character variable.

In a loop that executes as long as the user does not want to quit, use a switch statement to handle the different operations:

A '+' indicates the user wants to perform addition. Ask the user for the two numbers to be added. They are both integer values that must be stored in integer variables. Add the two numbers together and display the resulting sum. The display of the sum should also include the two numbers that were added together.

A '-' indicates the user wants to perform subtraction. Ask the user for the two numbers to be subtracted. They are both integer values that must be stored in integer variables. Subtract the second number from the first number and display the resulting difference. The display of the difference should also include the two numbers that were used in the subtraction.

A '*' indicates the user wants to perform multiplication. Ask the user for the two numbers to be multiplied. They are both integer values that must be stored in integer variables. Multiply the two numbers and display the resulting product. The display of the product should also include the two numbers that were multiplied together.

A '/' indicates the user wants to perform division. Ask the user for the first number. This is the dividend and is an integer value that must be stored in an integer variable. Ask the user for the second number. This is the divisor and is an integer value that must be stored in an integer variable. Divide the dividend by the divisor and display the resulting quotient and remainder. The display of the quotient and remainder should also include the two numbers that were used in the division.

A '^' indicates the user wants to raise a number to a power. Ask the user for the number to be raised to a power. This is an integer value that must be stored in an integer variable. Ask the user for the power. This is an integer value that must be stored in an integer variable. Write a loop that calculates the result of raising the number to the specified power.DO NOT USE THE pow FUNCTION. Display the result of raising a number to a power. The display of the product should also include the two numbers that were used in the calculation.

A '!' indicates the user wants to calculate the factorial of a number. Ask the user for the number that will be used in the calculation. This is an integer value that must be stored in an integer variable. Write a loop that calculates the factorial of the number. Remember that 0! is equal to 1. Otherwise, the factorial of a number is the product of the values from 1 through the number. For example, 4! is equal to 1 * 2 * 3 * 4, which is equal to 24. Display the calculated factorial. The display should also include the number that was used in the calculation.

Any other symbol indicates the user made a mistake. Display an error message that the user has entered an invalid operation. Note: in previous assignments, the user has been given the opportunity to re-enter a value after an error message has been displayed. Do not do that with this error message because that will happen later when the menu is displayed again.

Display the menu and ask the user what operation they would like to perform next.

Message Formats

Program Requirements

  1. The menu choice MUST be a character. The values used in the arithmetic MUST be integers.

  2. The display of the result of each calculation MUST include the value(s) used in the calculation.

  3. A value of 'q' or 'Q' should quit the program.

  4. Hand in a copy of the source code (the CPP file) on the autograder and Blackboard.

Output

What operation would you like to perform:
  + addition
  - subtraction
  * multiplication
  / division
  ^ number to power
  ! factorial
  q quit

Operation? +

What is the first number to add? 12

What is the second number to add? -8

12 + -8 = 4


What operation would you like to perform:
  + addition
  - subtraction
  * multiplication
  / division
  ^ number to power
  ! factorial
  q quit

Next Operation? -

What is the first number to subtract? 45

What is the second number to subtract? 62

45 - 62 = -17


What operation would you like to perform:
  + addition
  - subtraction
  * multiplication
  / division
  ^ number to power
  ! factorial
  q quit

Next Operation? *

What is the first number to multiply? 7

What is the second number to multiply? 4

7 * 4 = 28


What operation would you like to perform:
  + addition
  - subtraction
  * multiplication
  / division
  ^ number to power
  ! factorial
  q quit

Next Operation? /

What is the dividend? 8

What is the divisor? 12

8 / 12 = 0
8 % 12 = 8


What operation would you like to perform:
  + addition
  - subtraction
  * multiplication
  / division
  ^ number to power
  ! factorial
  q quit

Next Operation? ^

What is the base number? 4

What is the power? 6

4^6 = 4096


What operation would you like to perform:
  + addition
  - subtraction
  * multiplication
  / division
  ^ number to power
  ! factorial
  q quit

Next Operation? !

What is the number? 6

6! = 720


What operation would you like to perform:
  + addition
  - subtraction
  * multiplication
  / division
  ^ number to power
  ! factorial
  q quit

Next Operation? &

& is an invalid operation!


What operation would you like to perform:
  + addition
  - subtraction
  * multiplication
  / division
  ^ number to power
  ! factorial
  q quit

Next Operation? q