C++ Lab: 2B

Thu Aug 14 2025
Updated: Fri Aug 15 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Introduction

In this lab you will practice prompting the user for a value, reading the value as a double from the user/keyboard into a variable, using arithmetic expressions to calculate a value, and formatting the output using manipulators. Be sure to read this lab thoroughly, especially the Hand-in Procedure.

Lab

Write a complete program that:

  1. Creates a global constant to hold pi (π = 3.141592).

  2. Prompts the user to enter a radius and reads in a radius from the keyboard as a double.

  3. Calculates the circumference of a circle.

  4. Calculates the area of a circle.

  5. Writes the answers from (3) and (4) out to the display using <iomanip>. See the example output for formatting.

The equation for circumference of a circle is:

circumference=2πradiuscircumference=2*π*radius

The equation for area of a circle is:

area=πradius2area=π*radius^2

See the example output for examples and formatting. Output values should be fixed with 2 digits of precision. You will need to use <iomanip> to match the output.

Example Output

An example of an interaction with your program is shown below, your output should match these examples exactly.

Alexs-iMac:lab2b alex$ ./a.out Enter the radius of a circle **35.2458 +--------+---------------+---------+ | radius | circumference | area | +--------+---------------+---------+ | 35.25 | 221.46 | 3902.69 | +--------+---------------+---------+

See CodeGrade for more examples.

Note: The line breaks after the inputs in the example output formatting above are the default ones C++’s cin and getline() automatically apply after reading an input from the user. CodeGrade supplies input in a different manner (Linux redirection) to programs so in CodeGrade's auto tests there will not be line breaks after the inputs.

Submission Instructions

  1. Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.
  2. Submit: Your program source code must be submitted via CodeGrade as a properly named .cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.