Write a complete and correct C++ program that calculates and prints the diameter
ID: 3544268 • Letter: W
Question
Write a complete and correct C++ program that calculates and prints the diameter, the circumference, or the area of a circle, given the radius. The program inputs two data items. The first is a character - 'D' (for diameter), 'C' (for circumference), or 'A' (for area) - to indicate the calculation needed. The next data value is a floating-point number indicating the radius of the particular circle.
The output should be labeled appropriately and formatted to two decimal places.
For example, if the input is
A 6.75
your program should print something like this:
The area of a circle with radius 6.75 is 143.14.
Here are the formulas you need:
Diameter = 2*r
Circumference = 2*pi*r
Area of a circle = pi*r*r
where r is the radius. Use 3.14159265 for pi.
Error check the input data and display an error message if the choice of calculation is not an 'A', 'C' or 'D', or if the radius is not greater than zero.
Create an input file, called "circle.txt", containing input data as described.
The program should create an output file, called "results.txt", containing output data as described.
Use meaningful variable names, proper indentation, and appropriate comments.
Use the format and style described in the assignment guidelines.
In order to demonstrate that your program works, you must collect the output of your program.
Use five runs to demonstrate all components of your program work.
Explanation / Answer
# include using namespace std; void GetData (double); double CalcDiameter (double); // function prototype double CalcCircumference (double); double CalcArea (double); void PrintDiameter (double); void PrintCircumference (double); void PrintArea (double); int main () { char D, C, A, p;//declare variables cout > p; if (p != D || p != C || p != A) { cout p; } else if (p = D) { D = (CalcDiameter) (double) (PrintDiameter) (double); } else if (p = C) { C = (CalcCircumference) (double); (PrintCircumference) (double); } else { A = (CalcArea) (double); (PrintArea) (double); } return 0; } void GetData (double r)// get data from user { cout > r; if (r >= 0) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.